zena:crypto
import {…} from 'zena:crypto';
Cryptography: SHA-256, HMAC-SHA256, HKDF-SHA256, and ECDSA on P-256 and secp256k1. Pure Zena.
import {hmacSha256, p256, sha256Hex} from 'zena:crypto';
let digest = sha256Hex(bytes);
let mac = hmacSha256(key, message);
let signature = p256.sign(privateKey, message);
Classes
HkdfError
class HkdfError extends Error
An HKDF output length outside what RFC 5869 allows.
new(message: String)
EcdsaError
class EcdsaError extends Error
A private key out of range, a malformed public key, or a digest that is not 32 bytes.
new(message: String)
EcdsaCurve
final class EcdsaCurve
A curve to sign and verify on: p256 or secp256k1.
new(curve: Curve)
name: String { get; }
P-256 or secp256k1.
publicKey(privateKey: FixedArray<u8>, compressed: boolean = true): FixedArray<u8>
The SEC1 encoding of the public key for privateKey. Throws an
EcdsaError when the private key is not 32 bytes or not from 1 to
n - 1, as sign does.
compressPublicKey(key: FixedArray<u8>): FixedArray<u8>
The 33-byte compressed form of a SEC1 public key in either form.
Throws an EcdsaError when key is malformed or not on the curve.
decompressPublicKey(key: FixedArray<u8>): FixedArray<u8>
The 65-byte uncompressed form of a SEC1 public key, as compressPublicKey takes it.
sign(privateKey: FixedArray<u8>, message: FixedArray<u8>): FixedArray<u8>
The low-S signature of the SHA-256 hash of message.
signDigest(privateKey: FixedArray<u8>, digest: FixedArray<u8>): FixedArray<u8>
The low-S signature of a 32-byte SHA-256 digest, with the nonce derived from the key and the digest as RFC 6979 section 3.2 describes.
signatureFromDer(der: FixedArray<u8>): FixedArray<u8>
The 64-byte low-S signature an ASN.1 DER ECDSA-Sig-Value holds:
SEQUENCE { r INTEGER, s INTEGER }, as X.509, PKCS #11 and key
management services such as Cloud KMS write signatures. A high-S
signature, which they may give, is turned into its low-S twin, which
signs the same message. Throws an EcdsaError for anything else, or
an r or s that is 0 or not below the group order.
verify(key: FixedArray<u8>, message: FixedArray<u8>, signature: FixedArray<u8>, allowHighS: boolean = false): boolean
Whether signature is a valid signature of the SHA-256 hash of
message under the SEC1 public key key. False for a malformed
signature or key, a key not on the curve, and, unless allowHighS, a
high-S signature.
verifyDigest(key: FixedArray<u8>, digest: FixedArray<u8>, signature: FixedArray<u8>, allowHighS: boolean = false): boolean
verify for a SHA-256 digest of the message. Throws an EcdsaError
when digest is not 32 bytes.
Functions
sha256
function sha256(data: FixedArray<u8>): FixedArray<u8>
The 32-byte SHA-256 digest of data.
toHex
function toHex(bytes: FixedArray<u8>): String
bytes as lowercase hex; the same as hex.encode from zena:encoding.
sha256Hex
function sha256Hex(data: FixedArray<u8>): String
The digest as a lowercase hex string.
sha256HexOfString
function sha256HexOfString(s: String): String
The digest of a string's bytes as a lowercase hex string.
hmacSha256
function hmacSha256(key: FixedArray<u8>, message: FixedArray<u8>): FixedArray<u8>
The HMAC-SHA256 of message under key.
constantTimeEquals
function constantTimeEquals(a: FixedArray<u8>, b: FixedArray<u8>): boolean
Whether a and b hold the same bytes, taking as long whichever byte
differs, so that comparing a MAC or a secret's hash with the expected
one does not show, by its timing, how much of it was right. Only the
lengths, which are not secret, end it early.
hkdfExtract
function hkdfExtract(salt: FixedArray<u8>, ikm: FixedArray<u8>): FixedArray<u8>
HKDF's extract step: a 32-byte pseudorandom key from input keying material and an optional salt. RFC 5869 says an empty salt stands for 32 zero bytes; HMAC pads its key with zeros to a block either way, so passing the empty salt through gives the same result.
hkdfExpand
function hkdfExpand(prk: FixedArray<u8>, info: FixedArray<u8>, length: i32): FixedArray<u8>
HKDF's expand step: length bytes from a pseudorandom key and context
info. length is at most 255 * 32 bytes.
hkdf
function hkdf(salt: FixedArray<u8>, ikm: FixedArray<u8>, info: FixedArray<u8>, length: i32): FixedArray<u8>
HKDF-SHA256: extract, then expand to length bytes.
Variables
p256
let p256: EcdsaCurve
NIST P-256, also called secp256r1 and prime256v1.
secp256k1
let secp256k1: EcdsaCurve
secp256k1, the curve Bitcoin uses.