### Install Dependencies with PNPM Source: https://github.com/oslo-project/crypto/blob/main/CONTRIBUTING.md Use this command to install all necessary project dependencies. ```bash pnpm i ``` -------------------------------- ### Install @oslojs/crypto Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/index.md Install the @oslojs/crypto package using npm. ```bash npm i @oslojs/crypto ``` -------------------------------- ### Changeset Summary - New Feature Source: https://github.com/oslo-project/crypto/blob/main/CONTRIBUTING.md Example of a commit message summary for a new feature. ```markdown Feat: Add `greet()` ``` -------------------------------- ### HMAC Usage Example Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/hmac/hmac.md Demonstrates how to use the hmac() function with SHA256. Ensure the necessary imports for hmac and the chosen hash algorithm are included. ```typescript import { hmac } from "@oslojs/crypto/hmac"; import { SHA256 } from "@oslojs/crypto/sha2"; const hash = hmac(SHA256, key, message); ``` -------------------------------- ### Changeset Summary - Bug Fix Source: https://github.com/oslo-project/crypto/blob/main/CONTRIBUTING.md Example of a commit message summary for a bug fix. ```markdown Fix: Handle negative numbers in `sqrt()` ``` -------------------------------- ### Verify ECDSA Signature Example Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/verifyECDSASignature.md Demonstrates how to verify an ECDSA signature using the `verifyECDSA` function. Ensure the hash is generated using an appropriate algorithm for the curve, and the public key and signature components are correctly instantiated. ```ts import { verifyECDSA, p256, ECDSAPublicKey } from "@oslojs/crypto/ecdsa"; import { sha256 } from "@oslojs/crypto/sha2"; const hash = sha256(new TextEncoder().encode("hello")); const publicKey = new ECDSAPublicKey(p256, x, y); const valid = verifyECDSA(publicKey, hash, r, s); ``` -------------------------------- ### Implementing RandomReader with crypto.getRandomValues Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/random/RandomReader.md An example implementation of the RandomReader interface using the browser's built-in crypto API to generate random values. ```typescript import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes) { crypto.getRandomValues(bytes); } }; ``` -------------------------------- ### Direct Hashing Functions Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/sha.md Provides direct functions for hashing messages using various SHA algorithms. Simply pass the message to the corresponding function to get the hash digest. ```APIDOC ## Direct Hashing Functions ### Description Provides direct functions for hashing messages using various SHA algorithms. Simply pass the message to the corresponding function to get the hash digest. ### Available Functions - `sha1(message: string | Buffer | Uint8Array): string` - `sha224(message: string | Buffer | Uint8Array): string` - `sha256(message: string | Buffer | Uint8Array): string` - `sha384(message: string | Buffer | Uint8Array): string` - `sha512(message: string | Buffer | Uint8Array): string` - `sha512_224(message: string | Buffer | Uint8Array): string` - `sha512_256(message: string | Buffer | Uint8Array): string` - `sha3_224(message: string | Buffer | Uint8Array): string` - `sha3_256(message: string | Buffer | Uint8Array): string` - `sha3_384(message: string | Buffer | Uint8Array): string` - `sha3_512(message: string | Buffer | Uint8Array): string` - `shake128(message: string | Buffer | Uint8Array): string` - `shake256(message: string | Buffer | Uint8Array): string` ### Example ```ts import { sha256 } from "@oslojs/crypto/sha2"; const message = "Hello, world!"; const hash = sha256(message); console.log(hash); // Output: "dffd6021bb2bd5b0af676f97d450267724026604708708010666912120231367" ``` ``` -------------------------------- ### Run Tests and Build Package Source: https://github.com/oslo-project/crypto/blob/main/CONTRIBUTING.md Execute tests using `pnpm test` and build the package with `pnpm build`. ```bash pnpm test pnpm build ``` -------------------------------- ### Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHAKE128/index.md Initializes a new SHAKE128 instance with a specified digest size. ```APIDOC ## Constructor ### Signature ```ts function constructor(size: number): this; ``` ### Parameters - `size` (number): Digest size in bytes. ``` -------------------------------- ### Get HMAC Digest Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/hmac/HMAC/digest.md Call the digest() method to retrieve the final HMAC hash. This method returns a Uint8Array. ```typescript function digest(): Uint8Array; ``` -------------------------------- ### Create a New Changeset Source: https://github.com/oslo-project/crypto/blob/main/CONTRIBUTING.md Generate a changeset for your changes. Use 'minor' for new features and 'patch' for bug fixes. Multiple changesets can be created if needed. ```bash pnpm auri add minor ``` ```bash pnpm auri add patch ``` -------------------------------- ### SHA3_512 Methods and Properties Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHA3_512/index.md This section details the available methods and properties for the SHA3_512 implementation. ```APIDOC ## SHA3_512 Implements [`hash.Hash`](/reference/hash/Hash). Synchronous implementation of SHA3-512 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Methods - `digest()`: Computes the hash digest. - `update()`: Updates the hash state with the given data. ### Properties - `blockSize` (number): The block size of the hash algorithm in bytes. - `size` (number): The output size of the hash algorithm in bytes. ``` -------------------------------- ### SHAKE256 Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHAKE256/index.md Initializes a new SHAKE256 instance with a specified digest size. ```APIDOC ## Constructor SHAKE256 ### Description Initializes a new SHAKE256 instance with a specified digest size. ### Method `constructor(size: number)` ### Parameters #### Path Parameters - **size** (number) - Required - Digest size in bytes. ### Properties - **blockSize** (number) - **size** (number) ``` -------------------------------- ### Methods Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHAKE128/index.md Available methods for the SHAKE128 hash object. ```APIDOC ## Methods - `digest()`: Computes the hash digest. - `update()`: Updates the hash state with new data. ``` -------------------------------- ### RSAPublicKey Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/RSAPublicKey/index.md Constructs a new RSAPublicKey object. ```APIDOC ## RSAPublicKey Constructor ### Description Constructs a new RSAPublicKey object with the given modulus and public exponent. ### Parameters #### Parameters - **n** (bigint) - Required - The modulus of the RSA public key. - **e** (bigint) - Required - The public exponent of the RSA public key. ``` -------------------------------- ### SHA512 Methods and Properties Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/SHA512/index.md Provides details on the available methods and properties for the SHA512 hashing algorithm. ```APIDOC ## SHA512 ### Description Implements `hash.Hash`. Synchronous implementation of SHA-512 as specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Methods - `digest()`: Computes the SHA-512 hash of the data. - `update()`: Updates the SHA-512 hash with the given data. ### Properties - `blockSize` (number): The block size of the SHA-512 algorithm. - `size` (number): The output size of the SHA-512 hash in bytes. ``` -------------------------------- ### SHAKE Functions Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/index.md Implements NIST-approved SHAKE (Secure Hash Algorithm KECCAK) extendable-output functions. ```APIDOC ## SHAKE Functions ### Description Implements NIST-approved SHAKE (Secure Hash Algorithm KECCAK) extendable-output functions, allowing for variable-length output. ### Functions - `shake128()`: Computes the SHAKE128 extendable-output hash. - `shake256()`: Computes the SHAKE256 extendable-output hash. ``` -------------------------------- ### ECDSAPublicKey.encodePKIXCompressed() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/encodeKPKIXCompressed.md Encodes the public key using ASN.1 DER in a compressed PKIX format. ```APIDOC ## ECDSAPublicKey.encodePKIXCompressed() ### Description Encodes the public key with ASN.1 DER as specified in PKIX ([RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480)). The public key is compressed when using this API. ### Method Signature ```ts function encodePKIXCompressed(): Uint8Array; ``` ### Returns - `Uint8Array`: The compressed PKIX encoded public key. ``` -------------------------------- ### ECDSAPublicKey Methods Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/index.md Provides methods for encoding the public key in various formats and checking curve compatibility. ```APIDOC ## Methods - [`encodeSEC1Compressed()`](/reference/ecdsa/ECDSAPublicKey/encodeSEC1Compressed) - [`encodeSEC1Uncompressed()`](/reference/ecdsa/ECDSAPublicKey/encodeSEC1Uncompressed) - [`encodePKIXCompressed()`](/reference/ecdsa/ECDSAPublicKey/encodePKIXCompressed) - [`encodePKIXUncompressed()`](/reference/ecdsa/ECDSAPublicKey/encodePKIXUncompressed) - [`isCurve()`](/referecen/main/ECDSAPublicKey/isCurve) ``` -------------------------------- ### Generate Random Integer with Custom RandomReader Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/random/generateRandomIntegerNumber.md Demonstrates how to use `generateRandomIntegerNumber` with a custom `RandomReader` implementation. Ensure the `max` parameter is greater than 1 and less than or equal to `Number.MAX_SAFE_INTEGER`. ```ts import { generateRandomIntegerNumber } from "@oslojs/crypto/random"; import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes) { crypto.getRandomValues(bytes); } }; // random number from 0 to 9 const i = generateRandomIntegerNumber(random, 10); ``` -------------------------------- ### SHAKE256 Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHAKE256/index.md Initializes a new SHAKE256 instance with a specified digest size. The digest size determines the output length of the hash in bytes. ```typescript function constructor(size: number): this; ``` -------------------------------- ### RSAPublicKey Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/RSAPublicKey/index.md Constructs an RSAPublicKey with the given modulus and public exponent. Use this to create a new public key object. ```typescript function constructor(n: bigint, e: bigint): this; ``` -------------------------------- ### decodePKIXRSAPublicKey() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/index.md Decodes an RSA public key from PKIX format. ```APIDOC ## decodePKIXRSAPublicKey() ### Description Decodes an RSA public key from PKIX format. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an RSAPublicKey object. ### Response Example ```json { "example": "RSAPublicKey object" } ``` ``` -------------------------------- ### SHAKE Classes Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/index.md Offers class-based interfaces for SHAKE extendable-output functions for incremental hashing. ```APIDOC ## SHAKE Classes ### Description Offers class-based interfaces for SHAKE extendable-output functions, allowing for incremental hashing with variable-length output generation. ### Classes - `SHAKE128`: Class for computing SHAKE128 extendable-output hashes. - `SHAKE256`: Class for computing SHAKE256 extendable-output hashes. ``` -------------------------------- ### sha3_256() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_256.md Synchronously hashes data with SHA3-256. ```APIDOC ## sha3_256() ### Description Synchronously hashes data with SHA3-256 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Method Function call ### Signature ```ts function sha3_256(data: Uint8Array): Uint8Array; ``` ### Parameters #### Parameters - **data** (Uint8Array) - The data to be hashed. ``` -------------------------------- ### sha3_224() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_224.md Hashes input data synchronously using the SHA3-224 algorithm. ```APIDOC ## sha3_224() ### Description Synchronously hashes data with SHA3-224 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Method Function ### Signature ```ts function sha3_224(data: Uint8Array): Uint8Array; ``` ### Parameters #### Parameters - **data** (Uint8Array) - The input data to be hashed. ``` -------------------------------- ### HashAlgorithm Interface Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/hash/HashAlgorithm.md This interface represents a hash algorithm that can be instantiated to create new hash objects. It is intended for use within the crypto library. ```APIDOC ## HashAlgorithm Interface ### Description Defines the constructor for creating new instances of a hash algorithm. ### Type Definition ```ts interface HashAlgorithm { new (): $$Hash; } ``` ### Usage This interface is used internally by the crypto library to manage different hashing algorithms. It is not intended for direct external use. ``` -------------------------------- ### verifyRSASSAPKCS1v15Signature() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/index.md Verifies an RSASSA-PKCS1-v1_5 signature. ```APIDOC ## verifyRSASSAPKCS1v15Signature() ### Description Verifies an RSASSA-PKCS1-v1_5 signature against a message using an RSA public key. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns a boolean indicating whether the signature is valid. ### Response Example ```json { "example": "true or false" } ``` ``` -------------------------------- ### ECDSAPublicKey Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/index.md Initializes a new ECDSAPublicKey instance. Note that this constructor does not validate if the provided point (x, y) lies on the specified curve. ```APIDOC ## constructor ### Description Initializes a new ECDSAPublicKey instance. ### Parameters - `curve` (ECDSANamedCurve) - The named curve for the public key. - `x` (bigint) - The x-coordinate of the public key point. - `y` (bigint) - The y-coordinate of the public key point. ``` -------------------------------- ### decodePKIXRSAPublicKey() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/decodePKIXRSAPublicKey.md Decodes a DER encoded ASN.1 SubjectPublicKeyInfo structure. Only object identifier pkcs-1 1 (1.2.840.113549.1.1.1) is supported. Throws an Error if the public key is invalid. ```APIDOC ## decodePKIXRSAPublicKey() ### Description Decodes a DER encoded ASN.1 `SubjectPublicKeyInfo` structure. Only object identifier `pkcs-1 1` (1.2.840.113549.1.1.1) is supported. Throws an `Error` if the public key is invalid. ### Function Signature ```ts function decodePKIXRSAPublicKey(pkcs1: Uint8Array): RSAPublicKey; ``` ### Parameters #### Path Parameters - **pkcs1** (Uint8Array) - Required - The DER encoded ASN.1 `SubjectPublicKeyInfo` structure. ``` -------------------------------- ### RSAPublicKey.encodePKIX() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/RSAPublicKey/encodePKIX.md DER encodes the public key as ASN.1 `SubjectPublicKeyInfo` structure with an object identifier `pkcs-1 1` (1.2.840.113549.1.1.1). ```APIDOC ## RSAPublicKey.encodePKIX() ### Description DER encodes the public key as ASN.1 `SubjectPublicKeyInfo` structure with an object identifier `pkcs-1 1` (1.2.840.113549.1.1.1). ### Method Signature ```ts function encodePKIX(): Uint8Array; ``` ### Returns A `Uint8Array` containing the DER-encoded public key in SubjectPublicKeyInfo format. ``` -------------------------------- ### Updateable Hashing with SHA Algorithms Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/sha.md This approach is suitable for hashing data that arrives in chunks or needs to be processed incrementally. Instantiate the desired algorithm class, update it with data, and then retrieve the final digest. ```ts import { SHA1 } from "@oslojs/crypto/sha1"; import { SHA224, SHA256, SHA384, SHA512, SHA512_224, SHA512_256 } from "@oslojs/crypto/sha2"; import { SHA3_224, SHA3_256, SHA3_384, SHA3_512, SHAKE128, SHAKE256 } from "@oslojs/crypto/sha3"; const hash = new SHA256(); hash.update(data1); hash.update(data2); const digest = hash.digest(); ``` -------------------------------- ### Direct Hashing with SHA Algorithms Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/sha.md Use this method for simple, single-pass hashing of a message. Ensure the correct algorithm is imported from the respective module. ```ts import { sha1 } from "@oslojs/crypto/sha1"; import { sha224, sha256, sha384, sha512, sha512_224, sha512_256 } from "@oslojs/crypto/sha2"; import { sha3_224, sha3_256, sha3_384, sha3_512, shake128, shake256 } from "@oslojs/crypto/sha3"; const hash = sha256(message); ``` -------------------------------- ### SHA512_224 Hashing Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/SHA512_224/index.md This section details the methods and properties available for the SHA512_224 hashing algorithm. ```APIDOC ## SHA512_224 Hashing ### Description Provides a synchronous implementation of SHA-512/224 as specified in NIST FIPS 180-4, conforming to the `hash.Hash` interface. ### Methods - `digest()`: Computes the hash digest. - `update()`: Updates the hash state with the given data. ### Properties - `blockSize` (number): The block size of the algorithm in bytes. - `size` (number): The output size of the hash in bytes. ``` -------------------------------- ### sha3_384() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_384.md Hashes data synchronously using the SHA3-384 algorithm. ```APIDOC ## sha3_384() ### Description Synchronously hashes data with SHA3-384 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Method Function call ### Parameters #### Parameters - **data** (Uint8Array) - Required - The input data to be hashed. ``` -------------------------------- ### Verify RSASSA-PKCS1-v1_5 Signature Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/rsa.md Use this function to verify RSASSA-PKCS1-v1_5 signatures. It requires the message hash and the algorithm used for the hash. Ensure you import the necessary OIDs for SHA algorithms. ```typescript import { RSAPublicKey, verifyRSASSAPKCS1v15Signature, SHA256ObjectIdentifier } from "@oslojs/crypto/rsa"; import { sha256 } from "@oslojs/crypto/sha2"; const publicKey = new RSAPublicKey(n, e); const data = new TextEncoder().encode("hello world"); const hash = sha256(data); const valid = verifyRSASSAPKCS1v15Signature(publicKey, SHA256ObjectIdentifier, hash, signature); ``` -------------------------------- ### HashAlgorithm Interface Definition Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/hash/HashAlgorithm.md Defines the constructor signature for hash algorithm implementations. Use this interface to create new instances of hash algorithms. ```typescript //$ Hash=/reference/hash/Hash interface HashAlgorithm { new (): $$Hash; } ``` -------------------------------- ### hmac() Function Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/hmac/hmac.md This snippet shows the definition and usage of the hmac() function for hashing data with HMAC. ```APIDOC ## hmac() ### Description Synchronously hashes data with HMAC, specified in [NIST FIPS 198-1](https://csrc.nist.gov/pubs/fips/198-1/final). ### Definition ```ts //$ HashAlgorithm=/reference/hash/HashAlgorithm function hmac(Algorithm: hash.$$HashAlgorithm, key: Uint8Array, message: Uint8Array): Uint8Array; ``` ### Parameters - `Algorithm` - `key` - `message` ### Example ```ts import { hmac } from "@oslojs/crypto/hmac"; import { SHA256 } from "@oslojs/crypto/sha2"; const hash = hmac(SHA256, key, message); ``` ``` -------------------------------- ### shake256() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/shake256.md Hashes input data using the SHAKE256 algorithm and returns a digest of the specified size. ```APIDOC ## shake256() ### Description Synchronously hashes data with SHAKE256 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Method `shake256(size: number, data: Uint8Array): Uint8Array` ### Parameters #### Parameters - **size** (number) - Required - Digest size in bytes. - **data** (Uint8Array) - Required - The data to be hashed. ``` -------------------------------- ### constantTimeEqual Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/subtle/constantTimeEqual.md Compares two Uint8Arrays for equality in constant time. ```APIDOC ## constantTimeEqual() ### Description Compares two Uint8Arrays for equality in constant time. This function is designed to prevent timing side-channel attacks by ensuring the comparison takes the same amount of time regardless of where the first difference occurs. > Note: True constant time is difficult to achieve in JavaScript due to factors like JIT compilation and garbage collection. ### Signature ```ts function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean; ``` ### Parameters #### Path Parameters - **a** (Uint8Array) - The first Uint8Array to compare. - **b** (Uint8Array) - The second Uint8Array to compare. ### Returns - **boolean** - Returns `true` if the arrays are equal, `false` otherwise. ``` -------------------------------- ### Properties Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/SHAKE128/index.md Properties of the SHAKE128 hash object. ```APIDOC ## Properties ### Interface `Properties` ```ts interface Properties { blockSize: number; size: number; } ``` - `blockSize` (number): The block size of the hash function in bytes. - `size` (number): The output size of the hash function in bytes. ``` -------------------------------- ### sha1() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha1/sha1.md Synchronously hashes data using the SHA-1 algorithm. ```APIDOC ## sha1() ### Description Synchronously hashes data with SHA-1, specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Definition ```ts function sha1(data: Uint8Array): Uint8Array; ``` ### Parameters #### Parameters - `data` (Uint8Array) - The data to be hashed. ``` -------------------------------- ### Encode Public Key to SEC 1 Compressed Format Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/encodeSEC1Compressed.md Use this method to obtain the SEC 1 compressed representation of an ECDSAPublicKey. This format is often used in various cryptographic protocols. ```typescript function encodeSEC1Compressed(): Uint8Array; ``` -------------------------------- ### verifyRSASSAPSSSignature() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/index.md Verifies an RSASSA-PSS signature. ```APIDOC ## verifyRSASSAPSSSignature() ### Description Verifies an RSASSA-PSS signature against a message using an RSA public key. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns a boolean indicating whether the signature is valid. ### Response Example ```json { "example": "true or false" } ``` ``` -------------------------------- ### ECDSASignature Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSASignature/index.md Constructs a new ECDSASignature object with the given r and s components. ```APIDOC ## Constructor ECDSASignature ### Description Constructs a new ECDSASignature object. ### Parameters #### Parameters - **r** (bigint) - Required - Must be a positive integer over 0. - **s** (bigint) - Required - Must be a positive integer over 0. ``` -------------------------------- ### verifyRSASSAPKCS1v15Signature Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/verifyRSASSAPKCS1v15Signature.md Verifies an RSASSA-PKCS1-v1_5 signature. ```APIDOC ## verifyRSASSAPKCS1v15Signature() ### Description Verifies an RSASSA-PKCS1-v1_5 signature. ### Method Signature ```ts function verifyRSASSAPKCS1v15Signature( publicKey: RSAPublicKey, hashObjectIdentifier: string, hashed: Uint8Array, message: Uint8Array ): boolean; ``` ### Parameters - `publicKey` (RSAPublicKey) - The RSA public key to use for verification. - `hashObjectIdentifier` (string) - The object identifier of the hash algorithm used to hash the message. - `hashed` (Uint8Array) - The hashed message. - `message` (Uint8Array) - The original message that was signed. ``` -------------------------------- ### RandomReader.read() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/random/RandomReader.md Fills the given byte array with random bytes using a cryptographically secure random source. The function should throw if it fails to fill all the bytes. ```APIDOC ## read() ### Description Fills the given byte array with random bytes using a cryptographically secure random source. The function should throw if it fails to fill all the bytes. ### Method (Implicitly a method of the RandomReader interface) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ts import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes) { crypto.getRandomValues(bytes); } }; const buffer = new Uint8Array(16); random.read(buffer); ``` ### Response #### Success Response (No explicit return value, but the `bytes` array is filled) #### Response Example (N/A - operation modifies input buffer) ``` -------------------------------- ### verifyRSASSAPSSSignature() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/verifyRSASSAPSSSignature.md Verifies an RSASSA-PSS signature using MGF1. ```APIDOC ## verifyRSASSAPSSSignature() ### Description Verifies an RSASSA-PSS signature using MGF1. ### Method ```ts function verifyRSASSAPSSSignature( publicKey: $$RSAPublicKey, MessageHashAlgorithm: $$HashAlgorithm, MGF1HashAlgorithm: $$HashAlgorithm, saltLength: number, hashed: Uint8Array, signature: Uint8Array ): boolean; ``` ### Parameters - `publicKey` (RSAPublicKey) - The RSA public key. - `MessageHashAlgorithm` (HashAlgorithm) - The hash algorithm used for hashing the message. - `MGF1HashAlgorithm` (HashAlgorithm) - The hash algorithm used for MGF1 (usually the same as `MessageHashAlgorithm`). - `saltLength` (number) - The salt length in bytes. - `hashed` (Uint8Array) - The hashed message. - `signature` (Uint8Array) - The signature to verify. ``` -------------------------------- ### SHA3 Functions Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/index.md Provides direct access to NIST-approved SHA-3 hashing algorithms. ```APIDOC ## SHA3 Functions ### Description Provides direct access to NIST-approved SHA-3 hashing algorithms for fixed-length outputs. ### Functions - `sha3_224()`: Computes the SHA3-224 hash. - `sha3_256()`: Computes the SHA3-256 hash. - `sha3_384()`: Computes the SHA3-384 hash. - `sha3_512()`: Computes the SHA3-512 hash. ``` -------------------------------- ### ECDSASignature Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSASignature/index.md Initializes a new ECDSASignature instance. Both `r` and `s` must be positive integers greater than 0. ```typescript function constructor(r: bigint, s: bigint): this; ``` -------------------------------- ### ECDSAPublicKey.encodePKIXUncompressed() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/encodePKIXUncompressed.md Encodes the public key with ASN.1 DER as specified in PKIX ([RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480)). The public key is uncompressed when using this API. ```APIDOC ## ECDSAPublicKey.encodePKIXUncompressed() ### Description Encodes the public key with ASN.1 DER as specified in PKIX ([RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480)). The public key is uncompressed when using this API. ### Method Signature ```ts function encodePKIXUncompressed(): Uint8Array; ``` ### Returns - `Uint8Array`: The DER-encoded public key in PKIX format (uncompressed). ``` -------------------------------- ### sha384() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/sha384.md Computes the SHA-384 hash of the provided data synchronously. ```APIDOC ## sha384() ### Description Synchronously hashes data with SHA-384, specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Method ```ts function sha384(data: Uint8Array): Uint8Array; ``` ### Parameters #### Path Parameters - **data** (Uint8Array) - Required - The input data to be hashed. ``` -------------------------------- ### Updateable Hash Objects Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/sha.md Provides an alternative API using hash objects that allow for incremental updates. This is useful for hashing large messages or data streams by updating the hash object in chunks. ```APIDOC ## Updateable Hash Objects ### Description Provides an alternative API using hash objects that allow for incremental updates. This is useful for hashing large messages or data streams by updating the hash object in chunks. ### Available Classes - `SHA1` - `SHA224` - `SHA256` - `SHA384` - `SHA512` - `SHA512_224` - `SHA512_256` - `SHA3_224` - `SHA3_256` - `SHA3_384` - `SHA3_512` - `SHAKE128` - `SHAKE256` ### Methods - `update(data: string | Buffer | Uint8Array): void` - Updates the hash with the given data. - `digest(): string` - Computes and returns the hash digest. ### Example ```ts import { SHA256 } from "@oslojs/crypto/sha2"; const hash = new SHA256(); hash.update("Hello, "); hash.update("world!"); const digest = hash.digest(); console.log(digest); // Output: "dffd6021bb2bd5b0af676f97d450267724026604708708010666912120231367" ``` ``` -------------------------------- ### Encode Public Key to SEC 1 Uncompressed Format Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/encodeSEC1Uncompressed.md Use this method to obtain the SEC 1 uncompressed byte array representation of an ECDSAPublicKey. No specific imports are required beyond having an ECDSAPublicKey instance. ```typescript function encodeSEC1Uncompressed(): Uint8Array; ``` -------------------------------- ### sha512() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/sha512.md Synchronously hashes data with SHA-512. ```APIDOC ## sha512() ### Description Synchronously hashes data with SHA-512, specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Method ```ts function sha512(data: Uint8Array): Uint8Array; ``` ### Parameters #### Path Parameters - **data** (Uint8Array) - Required - The data to be hashed. ``` -------------------------------- ### generateRandomString Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/random-values.md Generates random strings of a specified length using a predefined set of characters. Requires a RandomReader. ```APIDOC ## generateRandomString ### Description Generates random strings of a specified length using a predefined set of characters. Requires a `RandomReader`. ### Method Signature - `generateRandomString(random: RandomReader, alphabet: string, length: number): string` ### Parameters - **random** (`RandomReader`) - Required - An implementation of the `RandomReader` interface. - **alphabet** (`string`) - Required - The set of characters to use for generating the string. - **length** (`number`) - Required - The desired length of the random string. ### Request Example ```ts import { generateRandomString } from "@oslojs/crypto/random"; // Assuming 'random' is a valid RandomReader implementation // 10-characters long string consisting of upper case letters const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const randomString = generateRandomString(random, alphabet, 10); ``` ``` -------------------------------- ### Synchronous SHA3-384 Hashing Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_384.md Use this function to synchronously hash a Uint8Array input into a Uint8Array output using the SHA3-384 algorithm. Ensure the input data is provided as a Uint8Array. ```typescript function sha3_384(data: Uint8Array): Uint8Array; ``` -------------------------------- ### sha1() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha1/index.md Provides a direct function to compute the SHA-1 hash of input data. ```APIDOC ## sha1() ### Description Computes the SHA-1 hash of the given data. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **hash** (string) - The SHA-1 hash of the input data. #### Response Example None ``` -------------------------------- ### sha3_512() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_512.md Computes the SHA3-512 hash of the provided data synchronously. ```APIDOC ## sha3_512() ### Description Synchronously hashes data with SHA3-512 as specified in [NIST FIPS 202](https://csrc.nist.gov/pubs/fips/202/final). ### Method Function call ### Signature ```ts function sha3_512(data: Uint8Array): Uint8Array; ``` ### Parameters #### Parameters - **data** (Uint8Array) - The input data to be hashed. ``` -------------------------------- ### Define RandomReader using Node's fillRandomSync Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/random-values.md Implements the `RandomReader` interface using Node.js's `fillRandomSync` function, suitable for older versions of Node.js. ```typescript import { fillRandomSync } from "node:crypto"; import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes: Uint8Array) { fillRandomSync(bytes); } }; ``` -------------------------------- ### generateRandomInteger and generateRandomIntegerNumber Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/random-values.md Generates random integers between 0 (inclusive) and a specified maximum (exclusive). Requires a RandomReader. ```APIDOC ## generateRandomInteger and generateRandomIntegerNumber ### Description Generates random integers between 0 (inclusive) and a specified maximum (exclusive). Requires a `RandomReader`. ### Method Signature - `generateRandomInteger(random: RandomReader, maximum: bigint): bigint` - `generateRandomIntegerNumber(random: RandomReader, maximum: number): number` ### Parameters #### `generateRandomInteger` - **random** (`RandomReader`) - Required - An implementation of the `RandomReader` interface. - **maximum** (`bigint`) - Required - The exclusive upper bound for the random integer. #### `generateRandomIntegerNumber` - **random** (`RandomReader`) - Required - An implementation of the `RandomReader` interface. - **maximum** (`number`) - Required - The exclusive upper bound for the random integer. ### Request Example ```ts import { generateRandomInteger, generateRandomIntegerNumber } from "@oslojs/crypto/random"; // Assuming 'random' is a valid RandomReader implementation // random number from 0 to 9 const numBigInt: bigint = generateRandomInteger(random, 10n); const numNumber: number = generateRandomIntegerNumber(random, 10); ``` ``` -------------------------------- ### Define RandomReader using Web Crypto API Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/random-values.md Implements the `RandomReader` interface using the globally available Web Crypto API. Suitable for modern runtimes like Node.js 20+, Bun, Deno, and Cloudflare Workers. ```typescript import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes: Uint8Array): void { crypto.getRandomValues(bytes); } }; ``` -------------------------------- ### SHA3 Classes Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/index.md Provides class-based interfaces for SHA-3 hashing algorithms, allowing for incremental hashing. ```APIDOC ## SHA3 Classes ### Description Provides class-based interfaces for SHA-3 hashing algorithms, enabling incremental updates and state management for hashing. ### Classes - `SHA3_224`: Class for computing SHA3-224 hashes. - `SHA3_256`: Class for computing SHA3-256 hashes. - `SHA3_384`: Class for computing SHA3-384 hashes. - `SHA3_512`: Class for computing SHA3-512 hashes. ``` -------------------------------- ### Define RandomReader using Node's webcrypto Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/random-values.md Implements the `RandomReader` interface using Node.js's `webcrypto` module, available in Node.js 18+. ```typescript import { webcrypto } from "node:crypto"; import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes: Uint8Array): void { webcrypto.getRandomValues(bytes); } }; ``` -------------------------------- ### SHA512_256 Interface Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/SHA512_256/index.md Provides methods for hashing data using the SHA512_256 algorithm and exposes properties related to its block size and output size. ```APIDOC ## SHA512_256 Implements [`hash.Hash`](/reference/hash/Hash). Synchronous implementation of SHA-512/256 as specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Methods - `digest()`: Computes the hash digest. - `update()`: Updates the hash state with the given data. ### Properties ```ts interface Properties { blockSize: number; size: number; } ``` - `blockSize` (number): The block size of the hash algorithm in bytes. - `size` (number): The output size of the hash algorithm in bytes. ``` -------------------------------- ### ECDSASignature.encodePKIX() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSASignature/encodePKIX.md Encodes the signature with ASN.1 DER as specified in PKIX. ```APIDOC ## ECDSASignature.encodePKIX() ### Description Encodes the signature with ASN.1 DER as specified in PKIX ([RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480)). ### Method ```ts function encodePKIX(): Uint8Array; ``` ### Returns - `Uint8Array`: The DER-encoded signature. ``` -------------------------------- ### Synchronous SHA3-256 Hashing Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_256.md Use this function to synchronously hash Uint8Array data using the SHA3-256 algorithm. Ensure the input is a Uint8Array. ```typescript function sha3_256(data: Uint8Array): Uint8Array; ``` -------------------------------- ### PKIX Public Key Encoding Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/rsa.md Encodes an RSA public key into PKIX format (ASN.1 DER encoding of a SubjectPublicKeyInfo structure). ```APIDOC ## PKIX Public Key Encoding ### Description Encodes an RSA public key into PKIX format (ASN.1 DER encoding of a `SubjectPublicKeyInfo` structure). ### Method `encodePKIX()` ### Usage Call this method on an `RSAPublicKey` instance. ### Request Example ```ts import { RSAPublicKey } from "@oslojs/crypto/rsa"; const publicKey = new RSAPublicKey(n, e); const pkix = publicKey.encodePKIX(); ``` ``` -------------------------------- ### decodePKIXECDSASignature() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/index.md Decodes an ECDSA signature from the PKIX format. ```APIDOC ## decodePKIXECDSASignature() ### Description Decodes an ECDSA signature from the PKIX format. ### Method Not applicable (function call) ### Parameters This function takes no explicit parameters in this documentation, but would typically expect a signature in PKIX format as input. ### Response Returns an ECDSA signature object. #### Success Response - **signature** (ECDSASignature) - The decoded ECDSA signature. ### Request Example ```javascript // Example usage (actual implementation details may vary) const signature = await decodePKIXECDSASignature(encodedSignature); ``` ### Response Example ```json { "r": "...", "s": "..." } ``` ``` -------------------------------- ### Encode RSA Public Key to PKIX Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/rsa.md Encodes an RSA public key into the PKIX (SubjectPublicKeyInfo) ASN.1 DER format. This is a standard format for public key distribution. ```typescript import { RSAPublicKey } from "@oslojs/crypto/rsa"; const publicKey = new RSAPublicKey(n, e); const pkix = publicKey.encodePKIX(); ``` -------------------------------- ### ECDSAPublicKey Constructor Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/index.md Defines the constructor for ECDSAPublicKey. It takes the ECDSA named curve, and the x and y coordinates as bigints. ```typescript function constructor(curve: $$ECDSANamedCurve, x: bigint, y: bigint): this; ``` -------------------------------- ### decodePKIXECDSAPublicKey() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/index.md Decodes an ECDSA public key from the PKIX format. ```APIDOC ## decodePKIXECDSAPublicKey() ### Description Decodes an ECDSA public key from the PKIX format. ### Method Not applicable (function call) ### Parameters This function takes no explicit parameters in this documentation, but would typically expect a public key in PKIX format as input. ### Response Returns an ECDSA public key object. #### Success Response - **publicKey** (ECDSAPublicKey) - The decoded ECDSA public key. ### Request Example ```javascript // Example usage (actual implementation details may vary) const publicKey = await decodePKIXECDSAPublicKey(encodedPublicKey); ``` ### Response Example ```json { "x": "...", "y": "...", "curve": "..." } ``` ``` -------------------------------- ### sha224() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/sha224.md Synchronously hashes data with SHA-224. ```APIDOC ## sha224() ### Description Synchronously hashes data with SHA-224, specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Definition ```ts function sha224(data: Uint8Array): Uint8Array; ``` ### Parameters #### Path Parameters - `data` (Uint8Array) - Required - The data to be hashed. ``` -------------------------------- ### SHA384 Class Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/index.md Provides an interface for computing SHA-384 hashes incrementally. ```APIDOC ## SHA384 Class ### Description Provides an interface for computing SHA-384 hashes incrementally. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### sha512_256() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/index.md Computes the SHA-512/256 hash of the input data. ```APIDOC ## sha512_256() ### Description Computes the SHA-512/256 hash of the input data. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### generateRandomInteger() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/random/generateRandomInteger.md Generates a random integer between 0 (inclusive) and a positive integer (exclusive). ```APIDOC ## generateRandomInteger() ### Description Generates a random integer between 0 (inclusive) and a positive integer (exclusive). ### Method Function ### Parameters #### Parameters - **random** (RandomReader) - Required - An object implementing the RandomReader interface for generating random bytes. - **max** (bigint) - Required - The exclusive upper bound for the random integer. Must be greater than 1. ### Example ```ts import { generateRandomInteger } from "@oslojs/crypto/random"; import type { RandomReader } from "@oslojs/crypto/random"; const random: RandomReader = { read(bytes) { crypto.getRandomValues(bytes); } }; // random number from 0 to 9 const i = generateRandomInteger(random, 10n); ``` ``` -------------------------------- ### PKIX Public Key Decoding Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/examples/rsa.md Decodes an RSA public key from PKIX format (ASN.1 DER encoding of a SubjectPublicKeyInfo structure). ```APIDOC ## PKIX Public Key Decoding ### Description Decodes an RSA public key from PKIX format (ASN.1 DER encoding of a `SubjectPublicKeyInfo` structure). ### Method `decodePKIXRSAPublicKey` ### Parameters - `pkix` (Uint8Array) - The PKIX encoded public key. ### Response - `RSAPublicKey` - The decoded RSA public key. ### Request Example ```ts import { decodePKIXRSAPublicKey } from "@oslojs/crypto/ecdsa"; try { const publicKey = decodePKIXRSAPublicKey(pkix); } catch { // Invalid key } ``` ``` -------------------------------- ### decodePKCS1RSAPublicKey() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/rsa/index.md Decodes an RSA public key from PKCS#1 format. ```APIDOC ## decodePKCS1RSAPublicKey() ### Description Decodes an RSA public key from PKCS#1 format. ### Method Not applicable (function call) ### Parameters None explicitly documented. ### Response Returns an RSAPublicKey object. ### Response Example ```json { "example": "RSAPublicKey object" } ``` ``` -------------------------------- ### ECDSAPublicKey.encodeSEC1Uncompressed() Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/ecdsa/ECDSAPublicKey/encodeSEC1Uncompressed.md Encodes the public key into a SEC 1 uncompressed format. This method returns the public key as a Uint8Array. ```APIDOC ## ECDSAPublicKey.encodeSEC1Uncompressed() ### Description Encodes the public key into a SEC 1 uncompressed format. ### Method Signature ```ts function encodeSEC1Uncompressed(): Uint8Array; ``` ### Returns - `Uint8Array`: The public key encoded in SEC 1 uncompressed format. ``` -------------------------------- ### Synchronous SHA3-224 Hashing Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha3/sha3_224.md Use this function to synchronously hash Uint8Array data with the SHA3-224 algorithm. Ensure the input is a Uint8Array. ```typescript function sha3_224(data: Uint8Array): Uint8Array; ``` -------------------------------- ### SHA384 Hashing Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/SHA384/index.md Provides methods to compute SHA384 hashes. It implements the standard hash.Hash interface. ```APIDOC ## SHA384 Hashing Implements [`hash.Hash`](/reference/hash/Hash). Synchronous implementation of SHA-384 as specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Methods - `digest()`: Computes and returns the SHA384 hash digest. - `update(data: Uint8Array)`: Updates the hash state with the given data. ### Properties - `blockSize`: The block size of the SHA384 algorithm in bytes. - `size`: The output size of the SHA384 hash in bytes. ``` -------------------------------- ### sha512_224() Function Source: https://github.com/oslo-project/crypto/blob/main/docs/pages/reference/sha2/sha512_224.md This function synchronously hashes data using the SHA-512/224 algorithm. ```APIDOC ## sha512_224() ### Description Synchronously hashes data with SHA-512/224, specified in [NIST FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf). ### Method Signature ```ts function sha512_224(data: Uint8Array): Uint8Array; ``` ### Parameters #### `data` - `data` (Uint8Array) - The input data to be hashed. ```