Skip to content

Cryptography API ​

GenerateRandomBytes Random ​

Generates random bytes.

Signature ​

ts
export function GenerateRandomBytes(length: number): Uint8Array;

Example ​

ts
import { GenerateRandomBytes } from "@fast-china/utils";

const result = GenerateRandomBytes(16);

Input ​

InputTypeRequired / defaultDescription
lengthnumberRequiredSafe integer from 0 to 65,536.

Returns ​

ValueTypeDescription
resultUint8ArrayNew Uint8Array.

FixedTimeEquals Compare ​

Compares two byte arrays without early exit.

Signature ​

ts
export function FixedTimeEquals(left: Uint8Array, right: Uint8Array): boolean;

Example ​

ts
import { FixedTimeEquals } from "@fast-china/utils";

const result = FixedTimeEquals(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]));

Input ​

InputTypeRequired / defaultDescription
leftUint8ArrayRequiredFirst byte sequence.
rightUint8ArrayRequiredSecond byte sequence.

Returns ​

ValueTypeDescription
resultbooleantrue if lengths and every byte match.

MD5Encrypt Hash ​

Computes an MD5 digest as lowercase hexadecimal text.

Signature ​

ts
export function MD5Encrypt(value: string): string;

Example ​

ts
import { MD5Encrypt } from "@fast-china/utils";

const result = MD5Encrypt("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text.

Returns ​

ValueTypeDescription
resultstring32-character lowercase hexadecimal digest.

SHA1Encrypt Hash ​

Computes a SHA-1 digest as uppercase hexadecimal text.

Signature ​

ts
export function SHA1Encrypt(value: string): string;

Example ​

ts
import { SHA1Encrypt } from "@fast-china/utils";

const result = SHA1Encrypt("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text.

Returns ​

ValueTypeDescription
resultstring40-character uppercase hexadecimal digest.

SHA256Bytes Hash ​

Computes a SHA-256 digest.

Signature ​

ts
export async function SHA256Bytes(value: string): Promise<Uint8Array>;

Example ​

ts
import { SHA256Bytes } from "@fast-china/utils";

const result = await SHA256Bytes("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 string or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>32-byte digest.

SHA256Encrypt Hash ​

Computes SHA-256 and formats it as hexadecimal.

Signature ​

ts
export async function SHA256Encrypt(value: string): Promise<string>;

Example ​

ts
import { SHA256Encrypt } from "@fast-china/utils";

const result = await SHA256Encrypt("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 string or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<string>64-character uppercase hexadecimal text.

SHA384Bytes Hash ​

Computes a SHA-384 digest.

Signature ​

ts
export async function SHA384Bytes(value: string): Promise<Uint8Array>;

Example ​

ts
import { SHA384Bytes } from "@fast-china/utils";

const result = await SHA384Bytes("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>48-byte digest.

SHA384Encrypt Hash ​

Computes SHA-384 and formats it as hexadecimal.

Signature ​

ts
export async function SHA384Encrypt(value: string): Promise<string>;

Example ​

ts
import { SHA384Encrypt } from "@fast-china/utils";

const result = await SHA384Encrypt("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<string>96-character uppercase hexadecimal digest.

SHA512Bytes Hash ​

Computes a SHA-512 digest.

Signature ​

ts
export async function SHA512Bytes(value: string): Promise<Uint8Array>;

Example ​

ts
import { SHA512Bytes } from "@fast-china/utils";

const result = await SHA512Bytes("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>64-byte digest.

SHA512Encrypt Hash ​

Computes SHA-512 and formats it as hexadecimal.

Signature ​

ts
export async function SHA512Encrypt(value: string): Promise<string>;

Example ​

ts
import { SHA512Encrypt } from "@fast-china/utils";

const result = await SHA512Encrypt("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes.

Returns ​

ValueTypeDescription
resultPromise<string>128-character uppercase hexadecimal digest.

HMACSHA256Encrypt MAC ​

Authenticates text with HMAC-SHA-256 and returns a hexadecimal tag.

Signature ​

ts
export async function HMACSHA256Encrypt(value: string, key: string): Promise<string>;

Example ​

ts
import { HMACSHA256Encrypt } from "@fast-china/utils";

const result = await HMACSHA256Encrypt("Fast 文档", "secret-key");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes to authenticate.
keystringRequiredNonempty UTF-8 key text or raw key bytes.

Returns ​

ValueTypeDescription
resultPromise<string>64-character lowercase hexadecimal authentication tag.

HMACSHA384Encrypt MAC ​

Authenticates text or bytes with HMAC-SHA-384 and returns a hexadecimal tag.

Signature ​

ts
export async function HMACSHA384Encrypt(value: string, key: string): Promise<string>;

Example ​

ts
import { HMACSHA384Encrypt } from "@fast-china/utils";

const result = await HMACSHA384Encrypt("Fast 文档", "secret-key");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes to authenticate.
keystringRequiredNonempty UTF-8 key text or raw key bytes.

Returns ​

ValueTypeDescription
resultPromise<string>96-character lowercase hexadecimal authentication tag.

HMACSHA512Encrypt MAC ​

Authenticates text or bytes with HMAC-SHA-512 and returns a hexadecimal tag.

Signature ​

ts
export async function HMACSHA512Encrypt(value: string, key: string): Promise<string>;

Example ​

ts
import { HMACSHA512Encrypt } from "@fast-china/utils";

const result = await HMACSHA512Encrypt("Fast 文档", "secret-key");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes to authenticate.
keystringRequiredNonempty UTF-8 key text or raw key bytes.

Returns ​

ValueTypeDescription
resultPromise<string>128-character lowercase hexadecimal authentication tag.

PBKDF2SHA256 Derive ​

Derives a key from a password using PBKDF2-HMAC-SHA-256.

Signature ​

ts
export async function PBKDF2SHA256(password: string, salt: Uint8Array, iterations = defaultPbkdf2Iterations, outputLength = 32): Promise<Uint8Array>;

Example ​

ts
import { PBKDF2SHA256 } from "@fast-china/utils";

const result = await PBKDF2SHA256("correct horse battery staple", new Uint8Array([1, 2, 3, 4]));

Input ​

InputTypeRequired / defaultDescription
passwordstringRequiredPassword of 1 to 1,024 UTF-8 bytes.
saltUint8ArrayRequiredSalt of at least 8 bytes.
iterationsunknownOptional; defaults to defaultPbkdf2IterationsIteration count from 100,000 to 5,000,000.
outputLengthunknownOptional; defaults to 32Output length from 1 to 1,024 bytes.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>Derived key of the requested length.

HashPasswordPBKDF2SHA256 Password ​

Generates a persistable PBKDF2-HMAC-SHA-256 password hash with random salt.

Signature ​

ts
export async function HashPasswordPBKDF2SHA256(password: string, iterations = defaultPbkdf2Iterations): Promise<string>;

Example ​

ts
import { HashPasswordPBKDF2SHA256 } from "@fast-china/utils";

const result = await HashPasswordPBKDF2SHA256("correct horse battery staple");

Input ​

InputTypeRequired / defaultDescription
passwordstringRequiredPassword of 1 to 1,024 UTF-8 bytes.
iterationsunknownOptional; defaults to defaultPbkdf2IterationsIteration count from 100,000 to 5,000,000.

Returns ​

ValueTypeDescription
resultPromise<string>Self-describing string containing the version, iteration count, 16-byte random salt, and 32-byte derived key.

VerifyPasswordPBKDF2SHA256 Verify ​

Verifies a generated password hash.

Signature ​

ts
export async function VerifyPasswordPBKDF2SHA256(password: string, passwordHash: string): Promise<boolean>;

Example ​

ts
import { VerifyPasswordPBKDF2SHA256 } from "@fast-china/utils";

const storedHash = await HashPasswordPBKDF2SHA256("correct horse battery staple");
const result = await VerifyPasswordPBKDF2SHA256("correct horse battery staple", storedHash);

Input ​

InputTypeRequired / defaultDescription
passwordstringRequiredPassword to verify.
passwordHashstringRequiredSelf-describing PBKDF2-HMAC-SHA-256 password hash.

Returns ​

ValueTypeDescription
resultPromise<boolean>true for a valid format and matching password; false for malformed hashes or incorrect passwords.

HKDFSHA256 Derive ​

Derives context-separated key material using RFC 5869 HKDF-SHA-256.

Signature ​

ts
export async function HKDFSHA256(
	inputKeyMaterial: Uint8Array,
	salt: Uint8Array = new Uint8Array(),
	info: Uint8Array = new Uint8Array(),
	outputLength = 32
): Promise<Uint8Array>;

Example ​

ts
import { HKDFSHA256 } from "@fast-china/utils";

const result = await HKDFSHA256(new Uint8Array([1, 2, 3]), new Uint8Array(), new Uint8Array(), 32);

Input ​

InputTypeRequired / defaultDescription
inputKeyMaterialUint8ArrayRequiredInput key material, such as a raw ECDH shared secret.
saltUint8ArrayOptional; defaults to new Uint8Array()Optional salt; empty values use RFC 5869 zero-salt semantics.
infoUint8ArrayOptional; defaults to new Uint8Array()Application, protocol, and key-purpose context.
outputLengthunknownOptional; defaults to 32Output length from 1 to 8,160 bytes.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>Derived key bound to salt and info.

AESEncrypt Encrypt ​

Encrypts UTF-8 text with AES-256 block encryption.

Signature ​

ts
export function AESEncrypt(
	dataStr: string,
	key: string,
	vector: string,
	cipherMode: AesCipherMode = "CBC",
	paddingMode: AesPaddingMode = "PKCS7"
): string | null;

Example ​

ts
import { AESEncrypt } from "@fast-china/utils";

const result = AESEncrypt("Fast", "0123456789abcdef0123456789abcdef", "0123456789abcdef");

Input ​

InputTypeRequired / defaultDescription
dataStrstringRequiredUTF-8 plaintext; blank text returns null.
keystringRequiredNonblank key text.
vectorstringRequiredNonblank initialization-vector text; ECB still requires this parameter to match the .NET signature.
cipherModeAesCipherModeOptional; defaults to "CBC"AES block mode; defaults to CBC.
paddingModeAesPaddingModeOptional; defaults to "PKCS7"AES padding; defaults to PKCS7.

Returns ​

ValueTypeDescription
resultstring | nullBase64 ciphertext; returns null if input, key, or IV is blank.

AESDecrypt Decrypt ​

Decrypts Base64 block ciphertext with AES-256.

Signature ​

ts
export function AESDecrypt(
	dataStr: string,
	key: string,
	vector: string,
	cipherMode: AesCipherMode = "CBC",
	paddingMode: AesPaddingMode = "PKCS7"
): DecodedText | null;

Example ​

ts
import { AESDecrypt, AESEncrypt } from "@fast-china/utils";

const ciphertext = AESEncrypt("Fast", "0123456789abcdef0123456789abcdef", "0123456789abcdef");
const result = ciphertext === null ? null : AESDecrypt(ciphertext, "0123456789abcdef0123456789abcdef", "0123456789abcdef");

Input ​

InputTypeRequired / defaultDescription
dataStrstringRequiredBase64 ciphertext; blank text returns null.
keystringRequiredKey text used for encryption.
vectorstringRequiredInitialization-vector text used for encryption.
cipherModeAesCipherModeOptional; defaults to "CBC"AES block mode; defaults to CBC.
paddingModeAesPaddingModeOptional; defaults to "PKCS7"AES padding; defaults to PKCS7.

Returns ​

ValueTypeDescription
resultDecodedText | nullRaw UTF-8 string usable directly or through .parseJson<Value>(); returns null if input, key, or IV is blank.

AESEncryptAuthenticated Encrypt ​

Normalizes a text key with SHA-256, then authenticates and encrypts UTF-8 text using AES-256-GCM.

Signature ​

ts
export async function AESEncryptAuthenticated(plaintext: string, key: string): Promise<string>;

Example ​

ts
import { AESEncryptAuthenticated } from "@fast-china/utils";

const result = await AESEncryptAuthenticated("Fast", "0123456789abcdef0123456789abcdef");

Input ​

InputTypeRequired / defaultDescription
plaintextstringRequiredUTF-8 text to encrypt.
keystringRequiredNonempty UTF-8 key text, internally normalized to a 32-byte SHA-256 digest.

Returns ​

ValueTypeDescription
resultPromise<string>Base64-encoded v1 authenticated AES-GCM payload.

AESDecryptAuthenticated Decrypt ​

Decrypts and authenticates payloads generated by the compatible encryptor or .NET AESEncryptAuthenticated.

Signature ​

ts
export async function AESDecryptAuthenticated(payload: string, key: string): Promise<DecodedText>;

Example ​

ts
import { AESDecryptAuthenticated, AESEncryptAuthenticated } from "@fast-china/utils";

const storedPayload = await AESEncryptAuthenticated("Fast", "0123456789abcdef0123456789abcdef");
const result = await AESDecryptAuthenticated(storedPayload, "0123456789abcdef0123456789abcdef");

Input ​

InputTypeRequired / defaultDescription
payloadstringRequiredBase64-encoded v1 AES-GCM binary payload.
keystringRequiredNonempty UTF-8 key text used for encryption.

Returns ​

ValueTypeDescription
resultPromise<DecodedText>Raw UTF-8 string usable directly or through explicit .parseJson<Value>().

AESEncryptWithPassword Encrypt ​

Derives a key with PBKDF2-HMAC-SHA-256, then authenticates and encrypts UTF-8 text with AES-256-GCM.

Signature ​

ts
export async function AESEncryptWithPassword(plaintext: string, password: string, iterations = defaultPbkdf2Iterations): Promise<string>;

Example ​

ts
import { AESEncryptWithPassword } from "@fast-china/utils";

const result = await AESEncryptWithPassword("Fast", "correct horse battery staple");

Input ​

InputTypeRequired / defaultDescription
plaintextstringRequiredRaw text without JSON inference; at most 8 MiB after UTF-8 encoding.
passwordstringRequiredSecret passphrase of 1 to 1024 UTF-8 bytes.
iterationsunknownOptional; defaults to defaultPbkdf2IterationsPBKDF2 work factor; defaults to 600,000.

Returns ​

ValueTypeDescription
resultPromise<string>Authenticated ciphertext string; repeated calls with identical input produce different results.

AESDecryptWithPassword Decrypt ​

Decrypts a generated v1 authenticated payload.

Signature ​

ts
export async function AESDecryptWithPassword(payload: string, password: string): Promise<DecodedText>;

Example ​

ts
import { AESDecryptWithPassword, AESEncryptWithPassword } from "@fast-china/utils";

const storedPayload = await AESEncryptWithPassword("Fast", "correct horse battery staple");
const result = await AESDecryptWithPassword(storedPayload, "correct horse battery staple");

Input ​

InputTypeRequired / defaultDescription
payloadstringRequiredUnmodified v1 payload, at most approximately 16 MiB of text.
passwordstringRequiredPassphrase used for encryption.

Returns ​

ValueTypeDescription
resultPromise<DecodedText>Raw UTF-8 string usable directly or through explicit .parseJson<Value>().

GenerateRSAKeyPair Keypair ​

Generates a PEM key pair usable with both RSA-OAEP/SHA-256 and RSA-PSS/SHA-256.

Signature ​

ts
export async function GenerateRSAKeyPair(modulusLength = 2048): Promise<PemKeyPair>;

Example ​

ts
import { GenerateRSAKeyPair } from "@fast-china/utils";

const result = await GenerateRSAKeyPair(2048);

Input ​

InputTypeRequired / defaultDescription
modulusLengthunknownOptional; defaults to 2048RSA modulus size in bits; defaults to 2,048 and must be a multiple of 256 no smaller than 2,048.

Returns ​

ValueTypeDescription
resultPromise<PemKeyPair>PEM pair containing an unencrypted PKCS#8 private key and SubjectPublicKeyInfo public key.

RSAEncryptOAEP Encrypt ​

Encrypts UTF-8 text with an RSA-OAEP/SHA-256 public key.

Signature ​

ts
export async function RSAEncryptOAEP(plaintext: string, publicKeyPem: string): Promise<string>;

Example ​

ts
import { GenerateRSAKeyPair, RSAEncryptOAEP } from "@fast-china/utils";

const { publicKey } = await GenerateRSAKeyPair();
const result = await RSAEncryptOAEP("Fast", publicKey);

Input ​

InputTypeRequired / defaultDescription
plaintextstringRequiredUTF-8 plaintext within the RSA-OAEP modulus size limit.
publicKeyPemstringRequiredSubjectPublicKeyInfo PEM public key.

Returns ​

ValueTypeDescription
resultPromise<string>Base64-encoded RSA ciphertext.

RSADecryptOAEP Decrypt ​

Decrypts Base64 ciphertext with an RSA-OAEP/SHA-256 private key.

Signature ​

ts
export async function RSADecryptOAEP(ciphertext: string, privateKeyPem: string): Promise<DecodedText>;

Example ​

ts
import { GenerateRSAKeyPair, RSADecryptOAEP, RSAEncryptOAEP } from "@fast-china/utils";

const { privateKey, publicKey } = await GenerateRSAKeyPair();
const storedCiphertext = await RSAEncryptOAEP("Fast", publicKey);
const result = await RSADecryptOAEP(storedCiphertext, privateKey);

Input ​

InputTypeRequired / defaultDescription
ciphertextstringRequiredBase64-encoded RSA ciphertext.
privateKeyPemstringRequiredUnencrypted PKCS#8 PEM private key.

Returns ​

ValueTypeDescription
resultPromise<DecodedText>Raw UTF-8 string usable directly or through explicit .parseJson<Value>().

RSASignPSS Sign ​

Signs text or bytes with an RSA-PSS/SHA-256 private key.

Signature ​

ts
export async function RSASignPSS(value: string, privateKeyPem: string): Promise<string>;

Example ​

ts
import { GenerateRSAKeyPair, RSASignPSS } from "@fast-china/utils";

const { privateKey } = await GenerateRSAKeyPair();
const result = await RSASignPSS("Fast", privateKey);

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes to sign.
privateKeyPemstringRequiredUnencrypted PKCS#8 PEM private key.

Returns ​

ValueTypeDescription
resultPromise<string>Base64-encoded RSA-PSS signature with a fixed 32-byte salt.

RSAVerifyPSS Verify ​

Verifies a Base64 signature with an RSA-PSS/SHA-256 public key.

Signature ​

ts
export async function RSAVerifyPSS(value: string, signature: string, publicKeyPem: string): Promise<boolean>;

Example ​

ts
import { GenerateRSAKeyPair, RSASignPSS, RSAVerifyPSS } from "@fast-china/utils";

const { privateKey, publicKey } = await GenerateRSAKeyPair();
const storedSignature = await RSASignPSS("Fast", privateKey);
const result = await RSAVerifyPSS("Fast", storedSignature, publicKey);

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes used when signing.
signaturestringRequiredBase64-encoded RSA-PSS signature.
publicKeyPemstringRequiredSubjectPublicKeyInfo PEM public key.

Returns ​

ValueTypeDescription
resultPromise<boolean>true if the signature matches the content and public key.

GenerateECDSAKeyPair Keypair ​

Generates an ECDSA PEM signing key pair.

Signature ​

ts
export async function GenerateECDSAKeyPair(namedCurve: EcNamedCurve = "P-256"): Promise<PemKeyPair>;

Example ​

ts
import { GenerateECDSAKeyPair } from "@fast-china/utils";

const result = await GenerateECDSAKeyPair("P-256");

Input ​

InputTypeRequired / defaultDescription
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve: P-256, P-384, or P-521.

Returns ​

ValueTypeDescription
resultPromise<PemKeyPair>PEM pair containing an unencrypted PKCS#8 private key and SubjectPublicKeyInfo public key.

ECDSASign Sign ​

Signs text or bytes with an ECDSA private key.

Signature ​

ts
export async function ECDSASign(value: string, privateKeyPem: string, namedCurve: EcNamedCurve = "P-256"): Promise<string>;

Example ​

ts
import { ECDSASign, GenerateECDSAKeyPair } from "@fast-china/utils";

const { privateKey } = await GenerateECDSAKeyPair();
const result = await ECDSASign("Fast", privateKey);

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes to sign.
privateKeyPemstringRequiredUnencrypted EC PKCS#8 PEM private key.
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve used by the private key.

Returns ​

ValueTypeDescription
resultPromise<string>Base64-encoded IEEE P1363 ECDSA signature.

ECDSAVerify Verify ​

Verifies a Base64 signature with an ECDSA public key.

Signature ​

ts
export async function ECDSAVerify(value: string, signature: string, publicKeyPem: string, namedCurve: EcNamedCurve = "P-256"): Promise<boolean>;

Example ​

ts
import { ECDSASign, ECDSAVerify, GenerateECDSAKeyPair } from "@fast-china/utils";

const { privateKey, publicKey } = await GenerateECDSAKeyPair();
const storedSignature = await ECDSASign("Fast", privateKey);
const result = await ECDSAVerify("Fast", storedSignature, publicKey);

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUTF-8 text or raw bytes used when signing.
signaturestringRequiredBase64-encoded IEEE P1363 ECDSA signature.
publicKeyPemstringRequiredEC SubjectPublicKeyInfo PEM public key.
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve used by the public key.

Returns ​

ValueTypeDescription
resultPromise<boolean>true if signature, content, public key, and curve match.

GenerateECDHKeyPair Keypair ​

Generates an ECDH PEM key-agreement pair.

Signature ​

ts
export async function GenerateECDHKeyPair(namedCurve: EcNamedCurve = "P-256"): Promise<PemKeyPair>;

Example ​

ts
import { GenerateECDHKeyPair } from "@fast-china/utils";

const result = await GenerateECDHKeyPair("P-256");

Input ​

InputTypeRequired / defaultDescription
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve: P-256, P-384, or P-521.

Returns ​

ValueTypeDescription
resultPromise<PemKeyPair>PEM pair containing an unencrypted PKCS#8 private key and SubjectPublicKeyInfo public key.

DeriveECDHSecret Derive ​

Derives a shared secret from the local ECDH private key and peer's public key.

Signature ​

ts
export async function DeriveECDHSecret(privateKeyPem: string, publicKeyPem: string, namedCurve: EcNamedCurve = "P-256"): Promise<Uint8Array>;

Example ​

ts
import { DeriveECDHSecret, GenerateECDHKeyPair } from "@fast-china/utils";

const own = await GenerateECDHKeyPair();
const peer = await GenerateECDHKeyPair();
const result = await DeriveECDHSecret(own.privateKey, peer.publicKey);

Input ​

InputTypeRequired / defaultDescription
privateKeyPemstringRequiredLocal unencrypted EC PKCS#8 PEM private key.
publicKeyPemstringRequiredPeer's EC SubjectPublicKeyInfo PEM public key.
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve used by both key pairs.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>Raw ECDH shared secret with the curve field length.

DeriveECDHKeySHA256 Derive ​

Derives a shared key with SHA-256 after ECDH.

Signature ​

ts
export async function DeriveECDHKeySHA256(privateKeyPem: string, publicKeyPem: string, namedCurve: EcNamedCurve = "P-256"): Promise<Uint8Array>;

Example ​

ts
import { DeriveECDHKeySHA256, GenerateECDHKeyPair } from "@fast-china/utils";

const own = await GenerateECDHKeyPair();
const peer = await GenerateECDHKeyPair();
const result = await DeriveECDHKeySHA256(own.privateKey, peer.publicKey);

Input ​

InputTypeRequired / defaultDescription
privateKeyPemstringRequiredLocal unencrypted EC PKCS#8 PEM private key.
publicKeyPemstringRequiredPeer's EC SubjectPublicKeyInfo PEM public key.
namedCurveEcNamedCurveOptional; defaults to "P-256"NIST curve used by both key pairs.

Returns ​

ValueTypeDescription
resultPromise<Uint8Array>32-byte shared key.