Static
getDetermines the JOSE algorithm identifier of the digital signature algorithm based on the alg
or
crv
property of a JWK.
If the alg
property is present, its value takes precedence and is returned. Otherwise, the
crv
property is used to determine the algorithm.
A JWK containing the alg
and/or crv
properties.
The name of the algorithm associated with the key.
CryptoUtils
const publicKey: Jwk = {
"kty": "OKP",
"crv": "Ed25519",
"x": "FEJG7OakZi500EydXxuE8uMc8uaAzEJkmQeG8khXANw"
}
const algorithm = getJoseSignatureAlgorithmFromPublicKey(publicKey);
console.log(algorithm); // Output: "EdDSA"
Error if the algorithm cannot be determined from the provided input.
Static
randomGenerates secure pseudorandom values of the specified length using
crypto.getRandomValues
, which defers to the operating system.
The number of bytes to generate.
A Uint8Array containing the generated random bytes.
CryptoUtils
This function is a wrapper around randomBytes
from the '@noble/hashes'
package. It's designed to be cryptographically strong, suitable for
generating initialization vectors, nonces, and other random values.
@noble/hashes on NPM for more information about the underlying implementation.
const bytes = randomBytes(32); // Generates 32 random bytes
Static
randomGenerates a secure random PIN (Personal Identification Number) of a specified length.
This function ensures that the generated PIN is cryptographically secure and uniformly distributed by using rejection sampling. It repeatedly generates random numbers until it gets one in the desired range [0, max]. This avoids bias introduced by simply taking the modulus or truncating the number.
Note: The function can generate PINs of 3 to 10 digits in length. Any request for a PIN outside this range will result in an error.
Example usage:
const pin = randomPin({ length: 4 });
console.log(pin); // Outputs a 4-digit PIN, e.g., "0231"
The options object containing the desired length of the generated PIN.
The desired length of the generated PIN. The value should be an integer between 3 and 8 inclusive.
A string representing the generated PIN. The PIN will be zero-padded to match the specified length, if necessary.
CryptoUtils
Will throw an error if the requested PIN length is less than 3 or greater than 8.
Static
randomGenerates a UUID (Universally Unique Identifier) using a cryptographically strong random number generator following the version 4 format, as specified in RFC 4122.
A version 4 UUID is a randomly generated UUID. The 13th character is set to '4' to denote version 4, and the 17th character is one of '8', '9', 'A', or 'B' to comply with the variant 1 format of UUIDs (the high bits are set to '10').
The UUID is a 36 character string, including hyphens, and looks like this: xxxxxxxx-xxxx-4xxx-axxx-xxxxxxxxxxxx
Note that while UUIDs are not guaranteed to be unique, they are practically unique" given the large number of possible UUIDs and the randomness of generation.
A string containing a randomly generated, 36 character long v4 UUID.
CryptoUtils
const uuid = randomUuid();
console.log(uuid); // Outputs a version 4 UUID, e.g., '123e4567-e89b-12d3-a456-426655440000'
A collection of cryptographic utility methods.