dymokomi/luce-crypto
Source of release 0.6.1
0.6.1 / tests / facade / main.luc
from crypto import Hasher, digest
from crypto import Hmac, secret_from_bytes, random_secret, authenticate, hkdf
from crypto import Argon2id
pub func main(arguments: list[str]) -> int!:
let state = Hasher()
state.update(b"a")
let first = state.digest()
assert(first.hexdigest() == "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb")
state.update(b"bc")
let result = state.digest()
assert(result.hexdigest() == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
let saved_bytes = result.bytes()
let saved_text = result.hexdigest()
result.close()
result.close()
state.reset()
assert(state.digest().hexdigest() == digest(b"").hexdigest())
state.close()
assert(saved_bytes.length == 32 and saved_text.length == 64)
assert(saved_text == digest(b"abc").hexdigest())
assert(first.hexdigest() == digest(b"a").hexdigest())
for algorithm in ["sha256", "sha384", "sha512"]:
let hasher = Hasher(algorithm)
hasher.update(b"binary\x00\xffinput")
assert(hasher.digest().bytes() == digest(b"binary\x00\xffinput", algorithm).bytes())
hasher.close()
let key = secret_from_bytes(b"Jefe")
assert(key.length() == 4 and key.matches(b"Jefe") and not key.matches(b"Jeff"))
let revealed = key.reveal()
let expected = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
assert(authenticate(key, b"what do ya want for nothing?").hexdigest() == expected)
let mac = Hmac(key)
mac.update(b"what do ya want ")
let verification = Hmac(key)
key.close()
assert(revealed == b"Jefe")
mac.update(b"for nothing?")
let tag = mac.finish()
assert(tag.hexdigest() == expected)
verification.update(b"what do ya want for nothing?")
assert(verification.verify(tag.bytes()))
let ikm = secret_from_bytes(b"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b")
let derived = hkdf(ikm, b"", b"", 42)
assert(derived.length() == 42)
assert(derived.matches(b"\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8"))
derived.close()
ikm.close()
let random = random_secret(32)
assert(random.length() == 32)
random.close()
let password = secret_from_bytes(b"password")
let configuration = Argon2id(32, 2, 4, 32, 4)
let password_key = configuration.derive(password, b"somesalt")
let password_key_copy = password_key.reveal()
password.close()
configuration.close()
assert(password_key.matches(b"\xd7\x4d\x7d\xb1\x54\xb3\x12\x93\x16\x25\xcd\xe5\xa5\x1f\x76\xbc\x52\x11\x3b\x4b\x05\x15\xaa\x94\x95\x22\x03\xb3\xcc\x45\xb8\x00"))
password_key.close()
assert(password_key_copy.length == 32)
let rfc_password = secret_from_bytes(b"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01")
let rfc_key = secret_from_bytes(b"\x03\x03\x03\x03\x03\x03\x03\x03")
let rfc_configuration = Argon2id(32, 3, 4, 32, 4)
let rfc_result = rfc_configuration.derive_keyed(rfc_password, rfc_key,
b"\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02", b"\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04")
assert(rfc_result.matches(b"\x0d\x64\x0d\xf5\x8d\x78\x76\x6c\x08\xc0\x37\xa3\x4a\x8b\x53\xc9\xd0\x1e\xf0\x45\x2d\x75\xb6\x5e\xb5\x25\x20\xe9\x6b\x01\xe6\x59"))
rfc_result.close()
rfc_password.close()
rfc_key.close()
rfc_configuration.close()
print("PASS Luce hash/keyed API, retained results, secret ownership, HMAC, RFC HKDF and parallel Argon2id")
return 0