thirdweb crypto
Secure, performant and cross-platform cryptography methods commonly used across other thirdweb packages.
Installation
npm install @thirdweb-dev/crypto
Available methods
Encryption
aesEncrypt(<plaintext>, <password>) | Encrypts a string/Uint8Array with AES-GCM | Promise<string> | WebCrypto (AES-GCM) |
aesDecrypt(<ciphertext>, <password>) | Decrypts a value encrypted via aesEncrypt | Promise<string> | WebCrypto (AES-GCM) |
aesDecryptCompat(<ciphertext>, <password>) | Same as aesDecrypt but also supports values encrypted via crypto-js (for backwards compatibility) | Promise<string> | WebCrypto (AES-GCM) |
Hashing
sha256(<value>) | Hashes a string/Uint8Array with SHA256 | Promise<Uint8Array> | WebCrypto |
sha256Hex(<value>) | Same as sha256 but reruns as hex string | Promise<string> | WebCrypto |
sha256Sync(<value>) | Same as sha256 but synchronous | Uint8Array | @noble/hashes |
sha256SyncHex(<value>) | Same as sha256Sync but reruns as hex string | string | @noble/hashes |
keccak256Sync(<value>) | Hashes a string/Uint8Array with Keccak256 | Uint8Array | js-sha3 |
keccak256SyncHex(<value>) | Same as keccak256Sync but returns as hex string | string | js-sha3 |
keccak256SyncHexPrefixed(<value>) | Same as keccak256Sync but returns as 0x prefixed hex string | 0x${string} | js-sha3 |
Initial Benchmarks
sha256 -> Uint8Array
ββββββββββββββββββββββββββββββββ¬ββββββββββ¬βββββββββββββββββββββ¬βββββββββ¬ββββββββββ
β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
ββββββββββββββββββββββββββββββββΌββββββββββΌβββββββββββββββββββββΌβββββββββΌββββββββββ€
β @thirdweb-dev/crypto (async) β 112,929 β 8855.08539999982 β Β±1.37% β 100000 β
β @thirdweb-dev/crypto (sync) β 55,004 β 18180.482820000594 β Β±1.02% β 100000 β
β crypto-js (sync) β 19,213 β 52047.35353999857 β Β±0.25% β 100000 β
ββββββββββββββββββββββββββββββββ΄ββββββββββ΄βββββββββββββββββββββ΄βββββββββ΄ββββββββββ
sha256 -> hex
ββββββββββββββββββββββββββββββββ¬ββββββββββ¬βββββββββββββββββββββ¬βββββββββ¬ββββββββββ
β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
ββββββββββββββββββββββββββββββββΌββββββββββΌβββββββββββββββββββββΌβββββββββΌββββββββββ€
β @thirdweb-dev/crypto (async) β 100,845 β 9916.125730001713 β Β±1.24% β 100000 β
β @thirdweb-dev/crypto (sync) β 53,751 β 18604.047449997142 β Β±0.92% β 100000 β
β crypto-js (sync) β 19,077 β 52419.03465000334 β Β±0.24% β 100000 β
ββββββββββββββββββββββββββββββββ΄ββββββββββ΄βββββββββββββββββββββ΄βββββββββ΄ββββββββββ
keccack256 -> Uint8Array
ββββββββββββββββββββββββ¬ββββββββββ¬βββββββββββββββββββββ¬βββββββββ¬ββββββββββ
β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
ββββββββββββββββββββββββΌββββββββββΌβββββββββββββββββββββΌβββββββββΌββββββββββ€
β @thirdweb-dev/crypto β 72,533 β 13786.719900000808 β Β±0.31% β 100000 β
β ethers@v5 β 71,891 β 13909.94663000107 β Β±0.33% β 100000 β
β @noble/hashes β 27,244 β 36704.73226000136 β Β±0.19% β 100000 β
β viem β 27,115 β 36879.07953999698 β Β±0.26% β 100000 β
ββββββββββββββββββββββββ΄ββββββββββ΄βββββββββββββββββββββ΄βββββββββ΄ββββββββββ
keccack256 -> hex
ββββββββββββββββββββββββ¬ββββββββββ¬ββββββββββββββββββββ¬βββββββββ¬ββββββββββ
β Task Name β ops/sec β Average Time (ns) β Margin β Samples β
ββββββββββββββββββββββββΌββββββββββΌββββββββββββββββββββΌβββββββββΌββββββββββ€
β @thirdweb-dev/crypto β 42,947 β 23284.22780000266 β Β±0.23% β 100000 β
β ethers@5 β 35,568 β 28114.98005000045 β Β±0.42% β 100000 β
β @noble/hashes β 15,149 β 66009.80448000187 β Β±0.26% β 100000 β
β viem β 15,217 β 65714.15843999518 β Β±0.25% β 100000 β
ββββββββββββββββββββββββ΄ββββββββββ΄ββββββββββββββββββββ΄βββββββββ΄ββββββββββ