The foundation for the internal standard library. (0.16.2)
This commit is contained in:
parent
71682293fe
commit
f0ed1d6761
13 changed files with 88 additions and 197 deletions
|
|
@ -277,6 +277,9 @@ fun md5_hex(hexStr)
|
|||
|
||||
// Class wrapper to avoid global name collisions and show class usage
|
||||
*/
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
class MD5()
|
||||
// MD5 constants (as fields)
|
||||
K = [
|
||||
|
|
@ -517,3 +520,8 @@ class MD5()
|
|||
bytes = this.from_hex(hexStr)
|
||||
digest = this.md5_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
||||
fun md5_str(this, str)
|
||||
bytes = string_to_bytes_ascii(str)
|
||||
digest = this.md5_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
// sha.sha1_hex(hexStr) -> digest hex string (lowercase)
|
||||
// sha.sha1_str("abc") -> digest hex string of ASCII bytes
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
class SHA1()
|
||||
// 32-bit helpers
|
||||
fun u32(this, x)
|
||||
|
|
@ -207,59 +209,7 @@ class SHA1()
|
|||
digest = this.sha1_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
||||
// ASCII string to bytes (printable ASCII 0x20..0x7E)
|
||||
fun string_to_bytes_ascii(this, s)
|
||||
str = to_string(s)
|
||||
out = []
|
||||
number i = 0
|
||||
// ASCII printable ranges
|
||||
P1 = " !\"#$%&'()*+,-./" // 32..47
|
||||
P2 = "0123456789" // 48..57
|
||||
P3 = ":;<=>?@" // 58..64
|
||||
P4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 65..90
|
||||
P5 = "[\\]^_`" // 91..96
|
||||
P6 = "abcdefghijklmnopqrstuvwxyz" // 97..122
|
||||
P7 = "{|}~" // 123..126
|
||||
while true
|
||||
ch = substr(str, i, 1)
|
||||
if (typeof(ch) != "String" || ch == "")
|
||||
break
|
||||
number code = -1
|
||||
idx = find(P1, ch)
|
||||
if (idx >= 0)
|
||||
code = 32 + idx
|
||||
else
|
||||
idx = find(P2, ch)
|
||||
if (idx >= 0)
|
||||
code = 48 + idx
|
||||
else
|
||||
idx = find(P3, ch)
|
||||
if (idx >= 0)
|
||||
code = 58 + idx
|
||||
else
|
||||
idx = find(P4, ch)
|
||||
if (idx >= 0)
|
||||
code = 65 + idx
|
||||
else
|
||||
idx = find(P5, ch)
|
||||
if (idx >= 0)
|
||||
code = 91 + idx
|
||||
else
|
||||
idx = find(P6, ch)
|
||||
if (idx >= 0)
|
||||
code = 97 + idx
|
||||
else
|
||||
idx = find(P7, ch)
|
||||
if (idx >= 0)
|
||||
code = 123 + idx
|
||||
else
|
||||
// non-printable -> 0
|
||||
code = 0
|
||||
push(out, code)
|
||||
i = i + 1
|
||||
return out
|
||||
|
||||
fun sha1_str(this, str)
|
||||
bytes = this.string_to_bytes_ascii(str)
|
||||
bytes = string_to_bytes_ascii(str)
|
||||
digest = this.sha1_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
// Example:
|
||||
// print(SHA256().sha256_hex("616263")) // "abc" => ba7816bf...
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
class SHA256()
|
||||
// 32-bit helpers
|
||||
fun u32(this, x)
|
||||
|
|
@ -260,60 +262,8 @@ class SHA256()
|
|||
digest = this.sha256_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
||||
// Convert a printable ASCII string (0x20..0x7E) to byte array
|
||||
fun string_to_bytes_ascii(this, s)
|
||||
str = to_string(s)
|
||||
out = []
|
||||
number i = 0
|
||||
// ASCII printable ranges
|
||||
P1 = " !\"#$%&'()*+,-./" // 32..47
|
||||
P2 = "0123456789" // 48..57
|
||||
P3 = ":;<=>?@" // 58..64
|
||||
P4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 65..90
|
||||
P5 = "[\\]^_`" // 91..96
|
||||
P6 = "abcdefghijklmnopqrstuvwxyz" // 97..122
|
||||
P7 = "{|}~" // 123..126
|
||||
while true
|
||||
ch = substr(str, i, 1)
|
||||
if (typeof(ch) != "String" || ch == "")
|
||||
break
|
||||
number code = -1
|
||||
idx = find(P1, ch)
|
||||
if (idx >= 0)
|
||||
code = 32 + idx
|
||||
else
|
||||
idx = find(P2, ch)
|
||||
if (idx >= 0)
|
||||
code = 48 + idx
|
||||
else
|
||||
idx = find(P3, ch)
|
||||
if (idx >= 0)
|
||||
code = 58 + idx
|
||||
else
|
||||
idx = find(P4, ch)
|
||||
if (idx >= 0)
|
||||
code = 65 + idx
|
||||
else
|
||||
idx = find(P5, ch)
|
||||
if (idx >= 0)
|
||||
code = 91 + idx
|
||||
else
|
||||
idx = find(P6, ch)
|
||||
if (idx >= 0)
|
||||
code = 97 + idx
|
||||
else
|
||||
idx = find(P7, ch)
|
||||
if (idx >= 0)
|
||||
code = 123 + idx
|
||||
else
|
||||
// non-printable -> 0
|
||||
code = 0
|
||||
push(out, code)
|
||||
i = i + 1
|
||||
return out
|
||||
|
||||
// Hash raw string bytes (printable ASCII). Example: sha256_str("616263") -> ee2109...
|
||||
fun sha256_str(this, str)
|
||||
bytes = this.string_to_bytes_ascii(str)
|
||||
bytes = string_to_bytes_ascii(str)
|
||||
digest = this.sha256_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@
|
|||
// ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a
|
||||
// 2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
class SHA512()
|
||||
// -------- hex helpers --------
|
||||
fun hex_val(this, ch)
|
||||
|
|
@ -360,60 +362,8 @@ class SHA512()
|
|||
digest = this.sha512_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
||||
// Convert a printable ASCII string (0x20..0x7E) to byte array
|
||||
fun string_to_bytes_ascii(this, s)
|
||||
str = to_string(s)
|
||||
out = []
|
||||
number i = 0
|
||||
// ASCII printable ranges
|
||||
P1 = " !\"#$%&'()*+,-./" // 32..47
|
||||
P2 = "0123456789" // 48..57
|
||||
P3 = ":;<=>?@" // 58..64
|
||||
P4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 65..90
|
||||
P5 = "[\\]^_`" // 91..96
|
||||
P6 = "abcdefghijklmnopqrstuvwxyz" // 97..122
|
||||
P7 = "{|}~" // 123..126
|
||||
while true
|
||||
ch = substr(str, i, 1)
|
||||
if (typeof(ch) != "String" || ch == "")
|
||||
break
|
||||
number code = -1
|
||||
idx = find(P1, ch)
|
||||
if (idx >= 0)
|
||||
code = 32 + idx
|
||||
else
|
||||
idx = find(P2, ch)
|
||||
if (idx >= 0)
|
||||
code = 48 + idx
|
||||
else
|
||||
idx = find(P3, ch)
|
||||
if (idx >= 0)
|
||||
code = 58 + idx
|
||||
else
|
||||
idx = find(P4, ch)
|
||||
if (idx >= 0)
|
||||
code = 65 + idx
|
||||
else
|
||||
idx = find(P5, ch)
|
||||
if (idx >= 0)
|
||||
code = 91 + idx
|
||||
else
|
||||
idx = find(P6, ch)
|
||||
if (idx >= 0)
|
||||
code = 97 + idx
|
||||
else
|
||||
idx = find(P7, ch)
|
||||
if (idx >= 0)
|
||||
code = 123 + idx
|
||||
else
|
||||
// non-printable -> 0
|
||||
code = 0
|
||||
push(out, code)
|
||||
i = i + 1
|
||||
return out
|
||||
|
||||
// Hash raw string bytes (printable ASCII). Example: sha512_str("616263") -> ee2109...
|
||||
fun sha512_str(this, str)
|
||||
bytes = this.string_to_bytes_ascii(str)
|
||||
bytes = string_to_bytes_ascii(str)
|
||||
digest = this.sha512_bytes(bytes)
|
||||
return this.bytes_to_hex(digest)
|
||||
|
|
|
|||
62
lib/strings.fun
Normal file
62
lib/strings.fun
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-01
|
||||
*/
|
||||
|
||||
// ASCII string to bytes (printable ASCII 0x20..0x7E)
|
||||
fun string_to_bytes_ascii(this, s)
|
||||
str = to_string(s)
|
||||
out = []
|
||||
number i = 0
|
||||
// ASCII printable ranges
|
||||
P1 = " !\"#$%&'()*+,-./" // 32..47
|
||||
P2 = "0123456789" // 48..57
|
||||
P3 = ":;<=>?@" // 58..64
|
||||
P4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 65..90
|
||||
P5 = "[\\]^_`" // 91..96
|
||||
P6 = "abcdefghijklmnopqrstuvwxyz" // 97..122
|
||||
P7 = "{|}~" // 123..126
|
||||
while true
|
||||
ch = substr(str, i, 1)
|
||||
if (typeof(ch) != "String" || ch == "")
|
||||
break
|
||||
number code = -1
|
||||
idx = find(P1, ch)
|
||||
if (idx >= 0)
|
||||
code = 32 + idx
|
||||
else
|
||||
idx = find(P2, ch)
|
||||
if (idx >= 0)
|
||||
code = 48 + idx
|
||||
else
|
||||
idx = find(P3, ch)
|
||||
if (idx >= 0)
|
||||
code = 58 + idx
|
||||
else
|
||||
idx = find(P4, ch)
|
||||
if (idx >= 0)
|
||||
code = 65 + idx
|
||||
else
|
||||
idx = find(P5, ch)
|
||||
if (idx >= 0)
|
||||
code = 91 + idx
|
||||
else
|
||||
idx = find(P6, ch)
|
||||
if (idx >= 0)
|
||||
code = 97 + idx
|
||||
else
|
||||
idx = find(P7, ch)
|
||||
if (idx >= 0)
|
||||
code = 123 + idx
|
||||
else
|
||||
// non-printable -> 0
|
||||
code = 0
|
||||
push(out, code)
|
||||
i = i + 1
|
||||
return out
|
||||
Loading…
Add table
Add a link
Reference in a new issue