1
0
Fork 0
forked from fun/fun

Some more stdlib refactoring removing duplicate code. (0.38.10)

This commit is contained in:
Johannes Findeisen 2026-02-09 01:43:06 +01:00
commit 8a1afc2aa1
7 changed files with 12 additions and 61 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(fun VERSION 0.38.9 LANGUAGES C) project(fun VERSION 0.38.10 LANGUAGES C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)

View file

@ -36,14 +36,6 @@ class AES256()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
i = 0
out = []
while i < len(arr)
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
// ---------- Finite field helpers (GF(2^8)) ---------- // ---------- Finite field helpers (GF(2^8)) ----------
fun b8(this, x) fun b8(this, x)
// clamp to 0..255 // clamp to 0..255
@ -245,7 +237,7 @@ class AES256()
pt = this.from_hex(pt_hex32) pt = this.from_hex(pt_hex32)
key = this.from_hex(key_hex64) key = this.from_hex(key_hex64)
ct = this.encrypt_block_bytes(pt, key) ct = this.encrypt_block_bytes(pt, key)
return this.bytes_to_hex(ct) return bytes_to_hex(ct)
fun encrypt_ecb_hex(this, hexStr, key_hex64) fun encrypt_ecb_hex(this, hexStr, key_hex64)
// Robust handling that avoids substr semantics ambiguity by working on bytes // Robust handling that avoids substr semantics ambiguity by working on bytes
@ -276,4 +268,4 @@ class AES256()
push(out_bytes, ct_blk[k]) push(out_bytes, ct_blk[k])
k = k + 1 k = k + 1
off = off + 16 off = off + 16
return this.bytes_to_hex(out_bytes) return bytes_to_hex(out_bytes)

View file

@ -59,21 +59,13 @@ class CRC32()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
i = 0
out = []
while i < len(arr)
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
fun u32_to_hex8(this, n) fun u32_to_hex8(this, n)
// big-endian printing (MSB first), common for CRC displays // big-endian printing (MSB first), common for CRC displays
b3 = this.and32(this.shr32(n, 24), 255) b3 = this.and32(this.shr32(n, 24), 255)
b2 = this.and32(this.shr32(n, 16), 255) b2 = this.and32(this.shr32(n, 16), 255)
b1 = this.and32(this.shr32(n, 8), 255) b1 = this.and32(this.shr32(n, 8), 255)
b0 = this.and32(n, 255) b0 = this.and32(n, 255)
return this.bytes_to_hex([b3, b2, b1, b0]) return bytes_to_hex([b3, b2, b1, b0])
// Bitwise update (no table needed) using reflected polynomial 0xEDB88320 // Bitwise update (no table needed) using reflected polynomial 0xEDB88320
POLY = 3988292384 // 0xEDB88320 POLY = 3988292384 // 0xEDB88320

View file

@ -90,14 +90,6 @@ class MD5()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
i = 0
out = []
while i < len(arr)
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
// MD5 aux // MD5 aux
fun F(this, x, y, z) fun F(this, x, y, z)
return this.or32(this.and32(x, y), this.and32(this.not32(x), z)) return this.or32(this.and32(x, y), this.and32(this.not32(x), z))
@ -213,9 +205,9 @@ class MD5()
fun md5_hex(this, hexStr) fun md5_hex(this, hexStr)
bytes = this.from_hex(hexStr) bytes = this.from_hex(hexStr)
digest = this.md5_bytes(bytes) digest = this.md5_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)
fun md5_str(this, str) fun md5_str(this, str)
bytes = string_to_bytes_ascii(str) bytes = string_to_bytes_ascii(str)
digest = this.md5_bytes(bytes) digest = this.md5_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)

View file

@ -53,14 +53,6 @@ class SHA1()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
i = 0
out = []
while i < len(arr)
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
// padding (SHA-1): append 0x80, then zeros until length 56 (mod 64), then 64-bit length big-endian // padding (SHA-1): append 0x80, then zeros until length 56 (mod 64), then 64-bit length big-endian
fun pad_bytes(this, bytes) fun pad_bytes(this, bytes)
L = len(bytes) L = len(bytes)
@ -164,9 +156,9 @@ class SHA1()
fun sha1_hex(this, hexStr) fun sha1_hex(this, hexStr)
bytes = this.from_hex(hexStr) bytes = this.from_hex(hexStr)
digest = this.sha1_bytes(bytes) digest = this.sha1_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)
fun sha1_str(this, str) fun sha1_str(this, str)
bytes = string_to_bytes_ascii(str) bytes = string_to_bytes_ascii(str)
digest = this.sha1_bytes(bytes) digest = this.sha1_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)

View file

@ -102,14 +102,6 @@ class SHA256()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
i = 0
out = []
while i < len(arr)
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
// padding (SHA-256) // padding (SHA-256)
fun pad_bytes(this, bytes) fun pad_bytes(this, bytes)
L = len(bytes) L = len(bytes)
@ -217,10 +209,10 @@ class SHA256()
fun sha256_hex(this, hexStr) fun sha256_hex(this, hexStr)
bytes = this.from_hex(hexStr) bytes = this.from_hex(hexStr)
digest = this.sha256_bytes(bytes) digest = this.sha256_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)
// Hash raw string bytes (printable ASCII). Example: sha256_str("616263") -> ee2109... // Hash raw string bytes (printable ASCII). Example: sha256_str("616263") -> ee2109...
fun sha256_str(this, str) fun sha256_str(this, str)
bytes = string_to_bytes_ascii(str) bytes = string_to_bytes_ascii(str)
digest = this.sha256_bytes(bytes) digest = this.sha256_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)

View file

@ -45,15 +45,6 @@ class SHA512()
i = i + 2 i = i + 2
return arr return arr
fun bytes_to_hex(this, arr)
out = []
number i = 0
number L = len(arr)
while i < L
push(out, two_hex(arr[i]))
i = i + 1
return join(out, "")
// -------- 32/64 helpers -------- // -------- 32/64 helpers --------
fun u32(this, x) fun u32(this, x)
m = 4294967296 m = 4294967296
@ -316,10 +307,10 @@ class SHA512()
fun sha512_hex(this, hexStr) fun sha512_hex(this, hexStr)
bytes = this.from_hex(hexStr) bytes = this.from_hex(hexStr)
digest = this.sha512_bytes(bytes) digest = this.sha512_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)
// Hash raw string bytes (printable ASCII). Example: sha512_str("616263") -> ee2109... // Hash raw string bytes (printable ASCII). Example: sha512_str("616263") -> ee2109...
fun sha512_str(this, str) fun sha512_str(this, str)
bytes = string_to_bytes_ascii(str) bytes = string_to_bytes_ascii(str)
digest = this.sha512_bytes(bytes) digest = this.sha512_bytes(bytes)
return this.bytes_to_hex(digest) return bytes_to_hex(digest)