e7e900973f
- lib/crypto/: 9-module crypto library (Hash, HMAC, Base64/URL, Random, AES, RSA, ECDSA, Ed25519, JWT) - rt/runtime.c: +346 lines OpenSSL crypto primitives (SHA-1/384/512, HMAC, Base64URL, AES-CBC/GCM, RSA, ECDSA, Ed25519) - apps/nexus/: multi-threaded HTTP/1.1 + HTTP/2 detect + WebSocket server - apps/jwt-pitbul/: JWT CLI tool (sign, verify, decode, keygen) - apps/boko-framework/: FastAPI-inspired async web framework - test_crypto/: crypto library test suite (23 tests)
93 lines
3.1 KiB
Plaintext
93 lines
3.1 KiB
Plaintext
// =============================================================================
|
|
// Std::Crypto::Hmac — HMAC-SHA256, HMAC-SHA384, HMAC-SHA512
|
|
// =============================================================================
|
|
module Std::Crypto::Hmac {
|
|
|
|
import Std::Mem::{Alloc, Free};
|
|
import Std::String::{String_Len};
|
|
|
|
extern func bux_hmac_sha256(key: String, keylen: int, msg: String, msglen: int, out: *void);
|
|
extern func bux_hmac_sha384(key: String, keylen: int, msg: String, msglen: int, out: *void);
|
|
extern func bux_hmac_sha512(key: String, keylen: int, msg: String, msglen: int, out: *void);
|
|
extern func bux_bytes_to_hex(data: *void, len: int) -> String;
|
|
extern func bux_base64_encode(data: String, len: int) -> String;
|
|
|
|
// --- HMAC-SHA256 ---
|
|
|
|
func Hmac_Sha256(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(32);
|
|
bux_hmac_sha256(key, kl, message, ml, buf);
|
|
let result: String = bux_bytes_to_hex(buf, 32);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
|
|
func Hmac_Sha256Raw(key: String, message: String, out: *void) {
|
|
bux_hmac_sha256(key, String_Len(key) as int, message, String_Len(message) as int, out);
|
|
}
|
|
|
|
func Hmac_Sha256Base64(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(32);
|
|
bux_hmac_sha256(key, kl, message, ml, buf);
|
|
let result: String = bux_base64_encode(buf as String, 32);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
|
|
// --- HMAC-SHA384 ---
|
|
|
|
func Hmac_Sha384(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(48);
|
|
bux_hmac_sha384(key, kl, message, ml, buf);
|
|
let result: String = bux_bytes_to_hex(buf, 48);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
|
|
func Hmac_Sha384Raw(key: String, message: String, out: *void) {
|
|
bux_hmac_sha384(key, String_Len(key) as int, message, String_Len(message) as int, out);
|
|
}
|
|
|
|
func Hmac_Sha384Base64(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(48);
|
|
bux_hmac_sha384(key, kl, message, ml, buf);
|
|
let result: String = bux_base64_encode(buf as String, 48);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
|
|
// --- HMAC-SHA512 ---
|
|
|
|
func Hmac_Sha512(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(64);
|
|
bux_hmac_sha512(key, kl, message, ml, buf);
|
|
let result: String = bux_bytes_to_hex(buf, 64);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
|
|
func Hmac_Sha512Raw(key: String, message: String, out: *void) {
|
|
bux_hmac_sha512(key, String_Len(key) as int, message, String_Len(message) as int, out);
|
|
}
|
|
|
|
func Hmac_Sha512Base64(key: String, message: String) -> String {
|
|
let kl: int = String_Len(key) as int;
|
|
let ml: int = String_Len(message) as int;
|
|
let buf: *void = Alloc(64);
|
|
bux_hmac_sha512(key, kl, message, ml, buf);
|
|
let result: String = bux_base64_encode(buf as String, 64);
|
|
Free(buf);
|
|
return result;
|
|
}
|
|
}
|