1
0
Fork 0
forked from fun/fun

Some stdlib refactoring removing duplicate code. (0.38.8)

This commit is contained in:
Johannes Findeisen 2026-02-09 00:29:50 +01:00
commit 54d94a1f3d
12 changed files with 34 additions and 437 deletions

View file

@ -23,49 +23,14 @@
// cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163
// 1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7
#include <hex.fun>
#include <strings.fun>
class SHA384()
// -------- hex helpers --------
fun hex_val(this, ch)
if (ch == "0")
return 0
else if (ch == "1")
return 1
else if (ch == "2")
return 2
else if (ch == "3")
return 3
else if (ch == "4")
return 4
else if (ch == "5")
return 5
else if (ch == "6")
return 6
else if (ch == "7")
return 7
else if (ch == "8")
return 8
else if (ch == "9")
return 9
else if (ch == "a" || ch == "A")
return 10
else if (ch == "b" || ch == "B")
return 11
else if (ch == "c" || ch == "C")
return 12
else if (ch == "d" || ch == "D")
return 13
else if (ch == "e" || ch == "E")
return 14
else if (ch == "f" || ch == "F")
return 15
else
return 0
fun byte_from_hex_pair(this, hh)
number hi = this.hex_val(substr(hh, 0, 1))
number lo = this.hex_val(substr(hh, 1, 1))
number hi = hex_val(substr(hh, 0, 1))
number lo = hex_val(substr(hh, 1, 1))
return hi * 16 + lo
fun from_hex(this, hex)
@ -79,24 +44,6 @@ class SHA384()
i = i + 2
return arr
fun two_hex(this, n)
n = n % 256
hexd = "0123456789abcdef"
number hi = (n / 16) % 16
number lo = n % 16
c1 = substr(hexd, hi, 1)
c2 = substr(hexd, lo, 1)
return join([c1, c2], "")
fun bytes_to_hex(this, arr)
out = []
number i = 0
number L = len(arr)
while i < L
push(out, this.two_hex(arr[i]))
i = i + 1
return join(out, "")
// -------- 32/64 helpers --------
fun u32(this, x)
m = 4294967296
@ -359,10 +306,10 @@ class SHA384()
fun sha384_hex(this, hexStr)
bytes = this.from_hex(hexStr)
digest = this.sha384_bytes(bytes)
return this.bytes_to_hex(digest)
return bytes_to_hex(digest)
// Hash raw string bytes (printable ASCII)
fun sha384_str(this, str)
bytes = string_to_bytes_ascii(str)
digest = this.sha384_bytes(bytes)
return this.bytes_to_hex(digest)
return bytes_to_hex(digest)