encoding
Two small, self-contained, dependency-free codecs. Both exist because net::ws needs them for the WebSocket handshake; both are useful on their own.
| Item | Import |
|---|---|
encode, decode | import std::encoding::base64; |
sha1 | import std::encoding::sha1; |
Base64
RFC 4648, standard alphabet, with padding.
import std::encoding::base64;
mut text: String = encode(bytes.as_ptr(), bytes.length());
match (decode(text.as_str())) {
Option::Some(raw) => { /* Array<u8> */ }
Option::None => { /* malformed input */ }
}
encode(data: u8*, len: u64) -> String encodes len bytes into a padded base64 string you own.
decode(source: Str) -> Option<Array<u8>> returns None on any invalid character or a malformed length. Whitespace is not tolerated — strip line breaks before calling if the input came from a wrapped MIME body.
SHA-1
RFC 3174. sha1(data: u8*, len: u64, out: u8*) -> void hashes len bytes and writes a 20-byte digest to out, which must point to at least that much space.
SHA-1 is cryptographically broken for collision resistance. It ships here only because the WebSocket handshake (RFC 6455) hard-codes it for the
Sec-WebSocket-Acceptvalue. Do not use it as a security primitive. Having a pure Cryo implementation is what keeps plainws://free of any external crypto dependency — no OpenSSL link required.
For hashing in the non-cryptographic sense — map keys, fingerprints — use core::hash. For key material, tokens, and nonces, use random::SecureRng.