UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 3.73 kB
import{x25519}from'@noble/curves/ed25519';import{falcon512padded}from'@noble/post-quantum/falcon.js';import{sha256}from'@noble/hashes/sha256';import{hmac}from'@noble/hashes/hmac';import{hkdf}from'@noble/hashes/hkdf';import{randomBytes}from'@noble/hashes/utils';import{ml_kem768}from'@noble/post-quantum/ml-kem.js';import a185a from'node:crypto';import{ROOT_KEY_SIZE,CHAIN_KEY_SIZE,MESSAGE_KEY_SIZE,DH_KEY_SIZE,AES_GCM_IV_SIZE}from'./types.js';export function generateRandomBytes(a){return randomBytes(a);}export function generateX25519KeyPair(){const a=randomBytes(DH_KEY_SIZE),b=x25519['getPublicKey'](a);return{'publicKey':b,'privateKey':a};}export function x25519DH(a,b){return x25519['getSharedSecret'](a,b);}export function generateFalcon512KeyPair(){const {publicKey:a,secretKey:b}=falcon512padded['keygen']();return{'publicKey':a,'privateKey':b};}export function falcon512Sign(a,b){return falcon512padded['sign'](b,a);}export function falcon512Verify(a,b,c){try{return falcon512padded['verify'](c,b,a);}catch{return![];}}export function generateMLKEMKeyPair(){const {publicKey:a,secretKey:b}=ml_kem768['keygen']();return{'publicKey':a,'secretKey':b};}export function mlkemEncapsulate(a){const {cipherText:b,sharedSecret:c}=ml_kem768['encapsulate'](a);return{'ciphertext':b,'sharedSecret':c};}export function mlkemDecapsulate(a,b){return ml_kem768['decapsulate'](a,b);}export function hkdfDerive(a,b,c,d){return hkdf(sha256,a,d||new Uint8Array(0x0),b,c);}export function hkdfChainKey(a,b){const c=new Uint8Array(a['length']+b['length']);c['set'](a),c['set'](b,a['length']);const d=hkdfDerive(c,new TextEncoder()['encode']('WowokPQChainKey'),ROOT_KEY_SIZE+CHAIN_KEY_SIZE);return{'rootKey':d['slice'](0x0,ROOT_KEY_SIZE),'chainKey':d['slice'](ROOT_KEY_SIZE,ROOT_KEY_SIZE+CHAIN_KEY_SIZE)};}export function hkdfMessageKey(a){const b=hkdfDerive(a,new TextEncoder()['encode']('WowokPQMessageKey'),MESSAGE_KEY_SIZE+CHAIN_KEY_SIZE);return{'messageKey':b['slice'](0x0,MESSAGE_KEY_SIZE),'nextChainKey':b['slice'](MESSAGE_KEY_SIZE,MESSAGE_KEY_SIZE+CHAIN_KEY_SIZE)};}export function hmacSha256(a,b){return hmac(sha256,a,b);}export function aesGcmEncrypt(a,b,c){const d=randomBytes(AES_GCM_IV_SIZE),e=Buffer['from'](a),f=Buffer['from'](d),g=Buffer['from'](b),h=Buffer['from'](c),i=a185a['createCipheriv']('aes-256-gcm',e,f);i['setAAD'](h);const j=Buffer['concat']([i['update'](g),i['final']()]),k=i['getAuthTag']();return{'ciphertext':new Uint8Array(j),'iv':d,'tag':new Uint8Array(k)};}export function aesGcmDecrypt(a,b,c,d,e){const f=Buffer['from'](a),g=Buffer['from'](c),h=Buffer['from'](b),i=Buffer['from'](d),j=Buffer['from'](e),k=a185a['createDecipheriv']('aes-256-gcm',f,g);k['setAAD'](j),k['setAuthTag'](i);const l=Buffer['concat']([k['update'](h),k['final']()]);return new Uint8Array(l);}export function sha256Hash(a){return sha256(a);}export function constantTimeEqual(c,d){if(c['length']!==d['length'])return![];let e=0x0;for(let f=0x0;f<c['length'];f++){e|=c[f]^d[f];}return e===0x0;}export function bytesToBase64(a){return Buffer['from'](a)['toString']('base64');}export function base64ToBytes(a){return new Uint8Array(Buffer['from'](a,'base64'));}export function bytesToHex(a){return Buffer['from'](a)['toString']('hex');}export function hexToBytes(a){return new Uint8Array(Buffer['from'](a,'hex'));}export function concatBytes(...a){const b=a['reduce']((e,f)=>e+f['length'],0x0),c=new Uint8Array(b);let d=0x0;for(const e of a){c['set'](e,d),d+=e['length'];}return c;}export function deriveRegistrationId(a){const b=(a[0x0]<<0x8|a[0x1])>>>0x0;return b===0x0?0x1:b;}export function generateX25519BindingSignature(a,b){return falcon512Sign(a,b);}export function verifyX25519BindingSignature(a,b,c){return falcon512Verify(a,b,c);}