1
0
Fork 0
forked from fun/fun

Added some crypto 100% implemented in Fun. (0.11.0)

This commit is contained in:
Johannes Findeisen 2025-09-29 02:36:57 +02:00
commit 12a5646d56
25 changed files with 1926 additions and 12 deletions

32
examples/crc32_demo.fun Normal file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env fun
/*
* 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 ISC license.
* https://opensource.org/license/isc-license-txt
*
* Added: 2025-09-29
*/
// Include the pure Fun CRC32 library
include "lib/crypt/crc32.fun"
// Create a Class-typed instance to prevent accidental shadowing
Class c32 = CRC32()
print(typeof(c32)) // should be "Class"
print("=== CRC32 demo (hex input) ===")
// "" (empty) -> 00000000
print(c32.crc32_hex(""))
// "abc" -> 352441c2
print(c32.crc32_hex("616263"))
// "123456789" -> cbf43926
print(c32.crc32_hex("313233343536373839"))
print("=== Done ===")

34
examples/md5_demo.fun Normal file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env fun
/*
* 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 ISC license.
* https://opensource.org/license/isc-license-txt
*
* Added: 2025-09-29
*/
// Include the pure Fun MD5 library
include "lib/crypt/md5.fun"
// create a hasher instance
md5 = MD5()
print("=== MD5 demo (hex input) ===")
// "abc" => 0x61 0x62 0x63
print(md5.md5_hex("616263")) // -> 900150983cd24fb0d6963f7d28e17f72
// empty string "" => hex ""
print(md5.md5_hex("")) // -> d41d8cd98f00b204e9800998ecf8427e
// "message digest"
print(md5.md5_hex("6d65737361676520646967657374")) // -> f96b697d7cb7938d525a2f31aaf161d0
// "abcdefghijklmnopqrstuvwxyz"
print(md5.md5_hex("6162636465666768696a6b6c6d6e6f707172737475767778797a")) // -> c3fcd3d76192e4007dfb496cca67e13b
print("=== Done ===")

33
examples/sha256_demo.fun Normal file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env fun
/*
* 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 ISC license.
* https://opensource.org/license/isc-license-txt
*
* Added: 2025-09-29
*/
// Include the pure Fun SHA-256 library
include "lib/crypt/sha256.fun"
sha = SHA256()
print("=== SHA-256 demo (hex input) ===")
// "" (empty)
print(sha.sha256_hex("")) // -> e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
// "abc"
print(sha.sha256_hex("616263")) // -> ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
// "message digest"
print(sha.sha256_hex("6d65737361676520646967657374")) // -> f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650
// "abcdefghijklmnopqrstuvwxyz"
print(sha.sha256_hex("6162636465666768696a6b6c6d6e6f707172737475767778797a")) // -> 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73
print("=== Done ===")

35
examples/sha512_demo.fun Normal file
View file

@ -0,0 +1,35 @@
#!/usr/bin/env fun
/*
* 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 ISC license.
* https://opensource.org/license/isc-license-txt
*
* Added: 2025-09-29
*/
include "lib/crypt/sha512.fun"
sha = SHA512()
print("=== SHA-512 demo (hex input) ===")
// "" (empty)
print(sha.sha512_hex("")) // -> cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce
// 47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
// "abc"
print(sha.sha512_hex("616263")) // -> ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a
// 2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
// "message digest"
print(sha.sha512_hex("6d65737361676520646967657374"))
// -> 107dbf389d9e9f71a3a95f6c055b9251
// bc5268c2be16d6c13492ea45b0199f33
// 09e16455ab1e96118e8a905d5597b721
// 3f36cc64dab6c0c5
print("=== Done ===")

View file

@ -0,0 +1,34 @@
#!/usr/bin/env fun
/*
* 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 ISC license.
* https://opensource.org/license/isc-license-txt
*
* Added: 2025-09-29
*/
// Demo: SHA-512 of raw strings (no hex decoding)
include "lib/crypt/sha512.fun"
sha = SHA512()
print("=== SHA-512 demo (raw string input via sha512_str) ===")
// "" (empty string)
print(sha.sha512_str("")) // -> cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce
// 47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
// "abc"
print(sha.sha512_str("abc")) // -> ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a
// 2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
// "616263" (the literal characters '6','1','6','2','6','3')
print(sha.sha512_str("616263")) // -> ee21095236a9c037f705f41ffa3cf60869891fcdb461c5c8fe50e5b1711a27b
// fc02de2e387228651bdd034113cc59af777fdb9c915b70fdbed0eeacf7113296b
print("=== Note ===")
print("sha512_str(\"616263\") hashes the text 616263; sha512_hex(\"616263\") hashes bytes 0x61 0x62 0x63 (abc).")