calog/examples/scripts/libraries/cryptoInJs.js

19 lines
622 B
JavaScript

// Crypto in JavaScript: SHA-256 hashing, a base64 encode/decode round-trip,
// and UUID generation via the calog crypto natives.
// Run: bin/calog examples/scripts/libraries/cryptoInJs.js
// NOTE: QuickJS has no console/print builtin -> use calogPrint.
const message = "hello world";
calogPrint("sha256(hello world):", cryptoHashSha256(message));
const encoded = cryptoBase64Encode(message);
const decoded = cryptoBase64Decode(encoded);
calogPrint("base64 encode:", encoded);
calogPrint("base64 decode:", decoded);
calogPrint("round-trip ok:", decoded === message);
calogPrint("uuid:", cryptoUuid());
calogExit(0);