@enbox/crypto 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
package/README.md
ADDED
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
# Web5 Crypto API
|
|
2
|
+
|
|
3
|
+
| A cryptography and JOSE library for building secure Web5 applications |
|
|
4
|
+
| --------------------------------------------------------------------- |
|
|
5
|
+
|
|
6
|
+
[![NPM Package][crypto-npm-badge]][crypto-npm-link]
|
|
7
|
+
[![NPM Downloads][crypto-downloads-badge]][crypto-npm-link]
|
|
8
|
+
|
|
9
|
+
[![Build Status][crypto-build-badge]][crypto-build-link]
|
|
10
|
+
[![Open Issues][crypto-issues-badge]][crypto-issues-link]
|
|
11
|
+
[![Code Coverage][crypto-coverage-badge]][crypto-coverage-link]
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
- [Web5 Crypto API](#web5-crypto-api)
|
|
16
|
+
- [Supported Algorithms \& Key Types](#supported-algorithms--key-types)
|
|
17
|
+
- [Extensions](#extensions)
|
|
18
|
+
- [Getting Started](#getting-started)
|
|
19
|
+
- [Node.js](#nodejs)
|
|
20
|
+
- [Web Browsers](#web-browsers)
|
|
21
|
+
- [React Native](#react-native)
|
|
22
|
+
- [Contributing](#contributing)
|
|
23
|
+
- [Core Concepts](#core-concepts)
|
|
24
|
+
- [Key URIs](#key-uris)
|
|
25
|
+
- [Using a Local KMS](#using-a-local-kms)
|
|
26
|
+
- [JSON Web Key (JWK)](#json-web-key-jwk)
|
|
27
|
+
- [Random Number Generation](#random-number-generation)
|
|
28
|
+
- [`randomBytes()`](#randombytes)
|
|
29
|
+
- [`randomUuid()`](#randomuuid)
|
|
30
|
+
- [Customization](#customization)
|
|
31
|
+
- [Persistent Local KMS Key Store](#persistent-local-kms-key-store)
|
|
32
|
+
- [Cryptographic Primitives](#cryptographic-primitives)
|
|
33
|
+
- [AES-CTR](#aes-ctr)
|
|
34
|
+
- [AES-GCM](#aes-gcm)
|
|
35
|
+
- [ConcatKDF](#concatkdf)
|
|
36
|
+
- [Ed25519](#ed25519)
|
|
37
|
+
- [PBKDF2](#pbkdf2)
|
|
38
|
+
- [secp256k1](#secp256k1)
|
|
39
|
+
- [SHA-256](#sha-256)
|
|
40
|
+
- [X25519](#x25519)
|
|
41
|
+
- [XChaCha20](#xchacha20)
|
|
42
|
+
- [XChaCha20-Poly1305](#xchacha20-poly1305)
|
|
43
|
+
- [Project Resources](#project-resources)
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
<a id="introduction"></a>
|
|
48
|
+
|
|
49
|
+
The Web5 Crypto API is a core component of the [Web5 JS](https://github.com/enboxorg/enbox/enbox)
|
|
50
|
+
ecosystem, providing the cryptography and JSON Object Signing and Encryption (JOSE) capabilities
|
|
51
|
+
essential for building secure applications and services with Decentralized Identifiers
|
|
52
|
+
([DID](https://www.w3.org/TR/did-core/)) and Verifiable Credentials
|
|
53
|
+
([VC](https://w3c.github.io/vc-data-model/)).
|
|
54
|
+
|
|
55
|
+
This JavaScript library was designed for modern development runtimes, including Node.js, web
|
|
56
|
+
browsers, and React Native. It provides cryptographic functionality for cipher, hash, and signature
|
|
57
|
+
algorithms and basic JOSE support for JSON Web Key (JWK). Additionally, it includes the crypto
|
|
58
|
+
interfaces and local Key Management System (KMS) implementation that are used by
|
|
59
|
+
[other libraries](https://github.com/enboxorg/enbox/enbox) in this monorepo.
|
|
60
|
+
|
|
61
|
+
### Supported Algorithms & Key Types
|
|
62
|
+
|
|
63
|
+
The following algorithms and key types are currently supported, with plans to expand its offerings
|
|
64
|
+
as the library progresses towards a 1.0 release.
|
|
65
|
+
|
|
66
|
+
| Capability | Details |
|
|
67
|
+
| -------------- | ----------------------------------------------- |
|
|
68
|
+
| Cipher | AES-CTR, AES-GCM, XChaCha20, XChaCha20-Poly1305 |
|
|
69
|
+
| Signature | ECDSA, EdDSA |
|
|
70
|
+
| Hash | SHA-256 |
|
|
71
|
+
| Key Derivation | ConcatKDF, ECDH, PBKDF2 |
|
|
72
|
+
| ECC Curves | Ed25519, secp256k1, X25519 |
|
|
73
|
+
|
|
74
|
+
### Extensions
|
|
75
|
+
|
|
76
|
+
Packages that extend the functionality of the `@enbox/crypto` library:
|
|
77
|
+
|
|
78
|
+
| Extension | Repository |
|
|
79
|
+
| ----------------- | ------------------------------------------------------ |
|
|
80
|
+
| AWS KMS extension | [enboxorg/enbox/crypto-aws-kms][crypto-aws-kms-repo-link] |
|
|
81
|
+
|
|
82
|
+
## Getting Started
|
|
83
|
+
|
|
84
|
+
The Web5 Crypto API is distributed as `@enbox/crypto` via [npmjs.com][crypto-npm-link],
|
|
85
|
+
[jsdelivr.com][crypto-jsdelivr-link], [unpkg.com][crypto-unpkg-link], and
|
|
86
|
+
[github.com][crypto-repo-link].
|
|
87
|
+
|
|
88
|
+
### Node.js
|
|
89
|
+
|
|
90
|
+
This library is designed and tested for the _active_ (`v20`) and _maintenance_
|
|
91
|
+
(`v18`) [LTS releases](https://nodejs.org/en/about/previous-releases) of Node.js
|
|
92
|
+
|
|
93
|
+
Install the latest version of `@enbox/crypto` using `npm` or your preferred package manager:
|
|
94
|
+
|
|
95
|
+
```shell
|
|
96
|
+
npm install @enbox/crypto
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Example ESM import:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { Ed25519 } from "@enbox/crypto";
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Example CJS require:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
const { Ed25519 } = require("@enbox/crypto");
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Web Browsers
|
|
112
|
+
|
|
113
|
+
A polyfilled distribution is published to [jsdelivr.com][crypto-jsdelivr-browser] and
|
|
114
|
+
[unpkg.com][crypto-unpkg-browser] that can imported in a module `<script>` tag:
|
|
115
|
+
|
|
116
|
+
```html
|
|
117
|
+
<!DOCTYPE html>
|
|
118
|
+
<html lang="en">
|
|
119
|
+
<body>
|
|
120
|
+
<script type="module">
|
|
121
|
+
// Example import from JSDELIVR
|
|
122
|
+
import { Ed25519 } from "https://cdn.jsdelivr.net/npm/@enbox/crypto/dist/browser.mjs";
|
|
123
|
+
</script>
|
|
124
|
+
</body>
|
|
125
|
+
</html>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
<a id="secure-context"></a>
|
|
129
|
+
|
|
130
|
+
> [!IMPORTANT]
|
|
131
|
+
> The `@enbox/crypto` library depends on the
|
|
132
|
+
> [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in web browsers.
|
|
133
|
+
> Web Crypto is available in all modern browsers but is accessible only in a
|
|
134
|
+
> [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
|
|
135
|
+
>
|
|
136
|
+
> This means you will not be able to use many `@enbox/crypto` features in a browser unless the page
|
|
137
|
+
> is served over `https://` or `wss://`. Locally-delivered resources such as those with
|
|
138
|
+
> `http://127.0.0.1`, `http://localhost`, or `http://*.localhost` URLs are also considered to have
|
|
139
|
+
> been delivered securely.
|
|
140
|
+
|
|
141
|
+
### React Native
|
|
142
|
+
|
|
143
|
+
For React Native, you may need a
|
|
144
|
+
[polyfill](https://github.com/LinusU/react-native-get-random-values) for `crypto.getRandomValues`.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import "react-native-get-random-values";
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Contributing
|
|
151
|
+
|
|
152
|
+
We welcome you to join our open source community. Whether you're new to open source or a seasoned
|
|
153
|
+
contributor, there's a place for you here. From coding to documentation, every contribution matters.
|
|
154
|
+
Check out our [contribution guide][contributing-link] for ways to get started.
|
|
155
|
+
|
|
156
|
+
For help, discussion about best practices, or to chat with others building on Web5 join our
|
|
157
|
+
[Discord Server][discord-link]:
|
|
158
|
+
|
|
159
|
+
[![discord-badge]][discord-link]
|
|
160
|
+
|
|
161
|
+
Remember, contributing is not just about code; it's about building together. Join us in shaping the
|
|
162
|
+
future of the Web!
|
|
163
|
+
|
|
164
|
+
## Core Concepts
|
|
165
|
+
|
|
166
|
+
### Key URIs
|
|
167
|
+
|
|
168
|
+
One of the core design principles for the SDKs in the Web5 ecosystem is the protection of private
|
|
169
|
+
key material. Instead of directly handling sensitive key information, our SDKs interact with
|
|
170
|
+
cryptographic keys through Key Management Systems (KMS) referenced by a unique identifier called a
|
|
171
|
+
**Key URI**. This approach ensures that private keys remain secure, while still allowing for
|
|
172
|
+
versatile cryptographic functionalities.
|
|
173
|
+
|
|
174
|
+
Each KMS assigns a unique identifier to a key when it is created. This identifier can be used to
|
|
175
|
+
form a Uniform Resource Identifier ([URI](https://datatracker.ietf.org/doc/html/rfc3986)) by adding
|
|
176
|
+
a prefix. The following table shows the format of supported **Key URIs**:
|
|
177
|
+
|
|
178
|
+
| Prefix | Key URI Format |
|
|
179
|
+
| --------- | -------------------------- |
|
|
180
|
+
| `urn:jwk` | `urn:jwk:<jwk-thumbprint>` |
|
|
181
|
+
|
|
182
|
+
All cryptographic keys are represented in JSON Web Key
|
|
183
|
+
([JWK](https://datatracker.ietf.org/doc/html/rfc7517)) format and the `jwk-thumbprint`, a
|
|
184
|
+
[standardized](https://datatracker.ietf.org/doc/html/rfc7638), deterministic, and unique hash of the
|
|
185
|
+
key, acts as a fingerprint, enabling consistent key referencing across all Web5 libraries without
|
|
186
|
+
exposing private key information.
|
|
187
|
+
|
|
188
|
+
### Using a Local KMS
|
|
189
|
+
|
|
190
|
+
This library includes a self-contained Key Management System (KMS) implementation that supports
|
|
191
|
+
generating keys, hashing, and creating/verifying digital signatures. Cryptographic operations are
|
|
192
|
+
performed within the confines of the local environment, without relying on external services. This
|
|
193
|
+
KMS is designed to be used by browser-based web apps or when testing backend services that depend on
|
|
194
|
+
cloud-based services (e.g., AWS KMS). Extensions that support external KMS services are
|
|
195
|
+
[listed here](#extensions).
|
|
196
|
+
|
|
197
|
+
Start by instantiating a local KMS implementation of the `CryptoApi` interface:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { LocalKeyManager } from "@enbox/crypto";
|
|
201
|
+
|
|
202
|
+
const kms = new LocalKeyManager();
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
An ephemeral, in-memory key store is used by default but any persistent store that implements the
|
|
206
|
+
[`KeyValueStore`](https://github.com/enboxorg/enbox/enbox/blob/5f364bc0d859e28f1388524ebe8ef152a71727c4/packages/common/src/types.ts#L4-L43)
|
|
207
|
+
interface can also be passed. See the [Persistent Key Store](#persistent-key-store) customization
|
|
208
|
+
for an example.
|
|
209
|
+
|
|
210
|
+
Generate a random private key:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
const privateKeyUri = await kms.generateKey({ algorithm: "Ed25519" });
|
|
214
|
+
console.log(privateKeyUri);
|
|
215
|
+
// Output: urn:jwk:8DaTzHZcvQXUVvl8ezQKgGQHza1hiOZlPkdrB55Vt6Q
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Create an EdDSA signature over arbitrary data using the private key:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
const data = new TextEncoder().encode("Message");
|
|
222
|
+
const signature = await kms.sign({
|
|
223
|
+
keyUri: privateKeyUri,
|
|
224
|
+
data,
|
|
225
|
+
});
|
|
226
|
+
console.log(signature);
|
|
227
|
+
// Output:
|
|
228
|
+
// Uint8Array(64) [
|
|
229
|
+
// 208, 172, 103, 148, 119, 138, 101, 95, 41, 218, 64,
|
|
230
|
+
// 178, 132, 102, 15, 126, 102, 85, 3, 174, 96, 140,
|
|
231
|
+
// 203, 58, 186, 72, 85, 64, 15, 250, 235, 12, 97,
|
|
232
|
+
// 247, 52, 31, 110, 58, 85, 191, 161, 253, 82, 4,
|
|
233
|
+
// 1, 4, 230, 135, 81, 145, 59, 128, 83, 106, 105,
|
|
234
|
+
// 135, 32, 124, 96, 0, 25, 41, 218, 75
|
|
235
|
+
// ]
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Get the public key in JWK format:
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const publicKey = await kms.getPublicKey({ keyUri: privateKeyUri });
|
|
242
|
+
console.log(publicKey);
|
|
243
|
+
// Output:
|
|
244
|
+
// {
|
|
245
|
+
// kty: "OKP",
|
|
246
|
+
// crv: "Ed25519",
|
|
247
|
+
// alg: "EdDSA",
|
|
248
|
+
// kid: "8DaTzHZcvQXUVvl8ezQKgGQHza1hiOZlPkdrB55Vt6Q",
|
|
249
|
+
// x: "6alAHg28tLuqWtaj0YOg7d5ySM4ERwPqQrLoy2pwdZk"
|
|
250
|
+
// }
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Verify the signature using the public key:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
const isValid = await kms.verify({
|
|
257
|
+
key: publicKey,
|
|
258
|
+
signature,
|
|
259
|
+
data,
|
|
260
|
+
});
|
|
261
|
+
console.log(isValid);
|
|
262
|
+
// Output: true
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Export the private key:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
const privateKey = await kms.exportKey({ keyUri });
|
|
269
|
+
console.log(privateKey);
|
|
270
|
+
// Output:
|
|
271
|
+
// {
|
|
272
|
+
// kty: "OKP",
|
|
273
|
+
// crv: "Ed25519",
|
|
274
|
+
// alg: "EdDSA",
|
|
275
|
+
// kid: "8DaTzHZcvQXUVvl8ezQKgGQHza1hiOZlPkdrB55Vt6Q",
|
|
276
|
+
// x: "6alAHg28tLuqWtaj0YOg7d5ySM4ERwPqQrLoy2pwdZk",
|
|
277
|
+
// d: "0xLuQyXFaWjrqp2o0orhwvwhtYhp2Z7KeRcioIs78CY"
|
|
278
|
+
// }
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Import a private key generated by another library:
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
import type { Jwk } from "@enbox/crypto";
|
|
285
|
+
|
|
286
|
+
const privateKey: Jwk = {
|
|
287
|
+
kty: "OKP",
|
|
288
|
+
crv: "Ed25519",
|
|
289
|
+
x: "0thnrEFw6MVSs1XFgd4OD-Yn05XGQv24hvskmGajtKQ",
|
|
290
|
+
d: "0xLuQyXFaWjrqp2o0orhwvwhtYhp2Z7KeRcioIs78CY",
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const keyUri = await kms.importKey({ key: privateKey });
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Compute the hash digest of arbitrary data:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
const data = new TextEncoder().encode("Message");
|
|
300
|
+
const hash = await kms.digest({ algorithm: "SHA-256", data });
|
|
301
|
+
console.log(hash);
|
|
302
|
+
// Output:
|
|
303
|
+
// Uint8Array(32) [
|
|
304
|
+
// 8, 187, 94, 93, 110, 170, 193, 4,
|
|
305
|
+
// 158, 222, 8, 147, 211, 14, 208, 34,
|
|
306
|
+
// 177, 164, 217, 181, 180, 141, 180, 20,
|
|
307
|
+
// 135, 31, 81, 201, 203, 53, 40, 61
|
|
308
|
+
// ]
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### JSON Web Key (JWK)
|
|
312
|
+
|
|
313
|
+
A JSON Web Key (JWK) is a [standardized](https://datatracker.ietf.org/doc/html/rfc7517) format for
|
|
314
|
+
representing cryptographic keys in a JSON data structure. The JWK format is used by all
|
|
315
|
+
[Web5 SDKs](https://github.com/enboxorg/enbox/sdk-development) to promote portability and
|
|
316
|
+
interoperability.
|
|
317
|
+
|
|
318
|
+
Defining a JWK:
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
import type { Jwk } from "@enbox/crypto";
|
|
322
|
+
|
|
323
|
+
const publicKey: Jwk = {
|
|
324
|
+
kty: "OKP",
|
|
325
|
+
crv: "Ed25519",
|
|
326
|
+
x: "KsZRg2-gm9MoSrZqN9aSHUv-zYW8ajgyCfKxLGWJ2DI",
|
|
327
|
+
};
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Computing the [thumbprint](https://datatracker.ietf.org/doc/html/rfc7638) of a JWK outputs a value
|
|
331
|
+
that can be used as a unique key identifier (`kid`) or to securely compare JWKs to determine if they
|
|
332
|
+
are equivalent:
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
import { computeJwkThumbprint } from "@enbox/crypto";
|
|
336
|
+
|
|
337
|
+
const thumbprint = await computeJwkThumbprint({ jwk: publicKey });
|
|
338
|
+
console.log(thumbprint);
|
|
339
|
+
// Output: VhWyK5rpk2u_51_KniJxjRhwTUOsL8BLuKJaqpNYuEA
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
The provided type guard functions can be used for type narrowing or to perform runtime verification
|
|
343
|
+
of a specific JWK key type:
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
isPublicJwk(publicKey); // true
|
|
347
|
+
isPrivateJwk(publicKey); // false
|
|
348
|
+
isOkpPublicJwk(publicKey); // true
|
|
349
|
+
isOkpPrivateJwk(publicKey); // false
|
|
350
|
+
isOctPrivateJwk(publicKey); // false
|
|
351
|
+
isEcPublicJwk(publicKey); // false
|
|
352
|
+
isEcPrivateJwk(publicKey); // false
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Random Number Generation
|
|
356
|
+
|
|
357
|
+
Cryptographically strong random number generation is vital in cryptography. The unpredictability of
|
|
358
|
+
these random numbers is crucial for creating secure cryptographic keys, initialization vectors, and
|
|
359
|
+
nonces (numbers used once). Their strength lies in their resistance to being guessed or replicated,
|
|
360
|
+
making them foundational to maintaining the integrity and confidentiality of cryptographic systems.
|
|
361
|
+
|
|
362
|
+
The Web5 Crypto API includes two utility functions for random number generation:
|
|
363
|
+
|
|
364
|
+
#### `randomBytes()`
|
|
365
|
+
|
|
366
|
+
The `randomBytes()` method generates cryptographically strong random values (CSPRNG) of the
|
|
367
|
+
specified length. The implementation relies on
|
|
368
|
+
[`crypto.getRandomValues`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues),
|
|
369
|
+
which defers to the runtime (browser, Node.js, etc.) for entropy. The pseudo-random number
|
|
370
|
+
generator algorithm may vary across runtimes, but is suitable for generating initialization vectors,
|
|
371
|
+
nonces and other random values.
|
|
372
|
+
|
|
373
|
+
> [!IMPORTANT]
|
|
374
|
+
> Use a cipher algorithm's `generateKey()` method to generate encryption keys rather than
|
|
375
|
+
> `randomBytes()`. This ensures that secrets used for encryption are guaranteed to be generated in a
|
|
376
|
+
> [secure context](#secure-context) and
|
|
377
|
+
> that the runtime uses the best
|
|
378
|
+
> [source of entropy](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes)
|
|
379
|
+
> available.
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
import { CryptoUtils } from "@enbox/crypto";
|
|
383
|
+
|
|
384
|
+
const nonce = CryptoUtils.randomBytes(24);
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
#### `randomUuid()`
|
|
388
|
+
|
|
389
|
+
The `randomUuid()` method generates a UUID (Universally Unique Identifier) using a cryptographically
|
|
390
|
+
strong random number generator (CSPRNG) following the version 4 format, as specified in
|
|
391
|
+
[RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
|
|
392
|
+
|
|
393
|
+
UUIDs are of a fixed size (128 bits), can guarantee uniqueness across space and time, and can be
|
|
394
|
+
generated without a centralized authority to administer them. Since UUIDs are unique and persistent,
|
|
395
|
+
they make excellent Uniform Resource Names ([URN](https://datatracker.ietf.org/doc/html/rfc2141)).
|
|
396
|
+
The following is an example of the string representation of a UUID as a URN:
|
|
397
|
+
`urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6`.
|
|
398
|
+
|
|
399
|
+
> [!NOTE]
|
|
400
|
+
> This function is available only in [secure contexts](#secure-context) (HTTPS and localhost).
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import { CryptoUtils } from "@enbox/crypto";
|
|
404
|
+
|
|
405
|
+
const uuid = CryptoUtils.randomUuid();
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Customization
|
|
409
|
+
|
|
410
|
+
### Persistent Local KMS Key Store
|
|
411
|
+
|
|
412
|
+
By default, `LocalKeyManager` uses an in-memory key store to simplify prototyping and testing.
|
|
413
|
+
To persist keys that are generated or imported, an implementation of the
|
|
414
|
+
[`KeyValueStore`](https://github.com/enboxorg/enbox/enbox/blob/5f364bc0d859e28f1388524ebe8ef152a71727c4/packages/common/src/types.ts#L4-L43)
|
|
415
|
+
interface can be passed.
|
|
416
|
+
|
|
417
|
+
For example, to use the [LevelDB](https://github.com/Level/level)-backed store from `@enbox/common`:
|
|
418
|
+
|
|
419
|
+
```ts
|
|
420
|
+
import type { KeyIdentifier, Jwk } from "@enbox/crypto";
|
|
421
|
+
import { Level } from "level";
|
|
422
|
+
import { LevelStore } from "@enbox/common";
|
|
423
|
+
|
|
424
|
+
const db = new Level<KeyIdentifier, Jwk>("db_location", {
|
|
425
|
+
valueEncoding: "json",
|
|
426
|
+
});
|
|
427
|
+
const keyStore = new LevelStore<KeyIdentifier, Jwk>({ db });
|
|
428
|
+
const kms = new LocalKeyManager({ keyStore });
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
## Cryptographic Primitives
|
|
432
|
+
|
|
433
|
+
This library encourages using its capabilities through concrete implementations of the `CryptoApi`
|
|
434
|
+
interface (e.g., [`LocalKeyManager`](#using-a-local-kms) or
|
|
435
|
+
[`AwsKeyManager`][crypto-aws-kms-repo-link]). These implementations provides high-level,
|
|
436
|
+
user-friendly access to a range of cryptographic functions. However, for developers requiring
|
|
437
|
+
lower-level control or specific customizations, the library also exposes its cryptographic
|
|
438
|
+
primitives directly. These primitives include a variety of cipher, hash, signature, key derivation,
|
|
439
|
+
and key conversion algorithms for advanced use cases.
|
|
440
|
+
|
|
441
|
+
> [!WARNING]
|
|
442
|
+
> While `@enbox/crypto` offers low-level cryptographic primitives, it's crucial to acknowledge the
|
|
443
|
+
> complexity and potential risks associated with their use. Secure key management and security
|
|
444
|
+
> system design are highly specialized fields, often requiring expert knowledge. Even with correct
|
|
445
|
+
> usage of cryptographic functions, designing a secure system poses significant challenges.
|
|
446
|
+
>
|
|
447
|
+
> It's important to approach the creation of secure systems with caution, seeking expert review and
|
|
448
|
+
> continual learning to ensure effectiveness and security. [Crypto 101](https://www.crypto101.io) is
|
|
449
|
+
> an introductory course on cryptography to _begin_ your learning journey, freely available for
|
|
450
|
+
> developers of all skill levels.
|
|
451
|
+
|
|
452
|
+
#### AES-CTR
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
import { AesCtr, CryptoUtils } from "@enbox/crypto";
|
|
456
|
+
|
|
457
|
+
// Key Generation
|
|
458
|
+
const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
459
|
+
const privateKey = await AesCtr.generateKey({ length });
|
|
460
|
+
|
|
461
|
+
// Encryption
|
|
462
|
+
const data = new TextEncoder().encode("Message");
|
|
463
|
+
const counter = CryptoUtils.randomBytes(16); // Initial value of the counter block
|
|
464
|
+
const encryptedData = await AesCtr.encrypt({
|
|
465
|
+
data,
|
|
466
|
+
counter,
|
|
467
|
+
key: privateKey,
|
|
468
|
+
length: 64, // Length of the counter in bits
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// Decryption
|
|
472
|
+
const decryptedData = await AesCtr.decrypt({
|
|
473
|
+
data: encryptedData,
|
|
474
|
+
counter,
|
|
475
|
+
key: privateKey,
|
|
476
|
+
length: 64, // Length of the counter in bits
|
|
477
|
+
});
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
#### AES-GCM
|
|
481
|
+
|
|
482
|
+
```ts
|
|
483
|
+
import { AesGcm, CryptoUtils } from "@enbox/crypto";
|
|
484
|
+
|
|
485
|
+
// Key Generation
|
|
486
|
+
const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
487
|
+
const privateKey = await AesGcm.generateKey({ length });
|
|
488
|
+
|
|
489
|
+
// Encryption
|
|
490
|
+
const data = new TextEncoder().encode("Message");
|
|
491
|
+
const iv = CryptoUtils.randomBytes(12); // Initialization vector
|
|
492
|
+
const encryptedData = await AesGcm.encrypt({
|
|
493
|
+
data,
|
|
494
|
+
iv,
|
|
495
|
+
key: privateKey,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
// Decryption
|
|
499
|
+
const decryptedData = await AesGcm.decrypt({
|
|
500
|
+
data: encryptedData,
|
|
501
|
+
iv,
|
|
502
|
+
key: privateKey,
|
|
503
|
+
});
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
#### ConcatKDF
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
import { ConcatKdf, CryptoUtils } from "@enbox/crypto";
|
|
510
|
+
|
|
511
|
+
// Key Derivation
|
|
512
|
+
const derivedKeyingMaterial = await ConcatKdf.deriveKey({
|
|
513
|
+
sharedSecret: CryptoUtils.randomBytes(32),
|
|
514
|
+
keyDataLen: 128,
|
|
515
|
+
otherInfo: {
|
|
516
|
+
algorithmId: "A128GCM",
|
|
517
|
+
partyUInfo: "Alice",
|
|
518
|
+
partyVInfo: "Bob",
|
|
519
|
+
suppPubInfo: 128,
|
|
520
|
+
},
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
#### Ed25519
|
|
525
|
+
|
|
526
|
+
```ts
|
|
527
|
+
import { Ed25519 } from "@enbox/crypto";
|
|
528
|
+
|
|
529
|
+
// Key Generation
|
|
530
|
+
const privateKey = await Ed25519.generateKey();
|
|
531
|
+
|
|
532
|
+
// Public Key Derivation
|
|
533
|
+
const publicKey = await Ed25519.computePublicKey({ key: privateKey });
|
|
534
|
+
|
|
535
|
+
// EdDSA Signing
|
|
536
|
+
const signature = await Ed25519.sign({
|
|
537
|
+
key: privateKey,
|
|
538
|
+
data: new TextEncoder().encode("Message"),
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// EdDSA Signature Verification
|
|
542
|
+
const isValid = await Ed25519.verify({
|
|
543
|
+
key: publicKey,
|
|
544
|
+
signature: signature,
|
|
545
|
+
data: new TextEncoder().encode("Message"),
|
|
546
|
+
});
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
#### PBKDF2
|
|
550
|
+
|
|
551
|
+
```ts
|
|
552
|
+
import { Pbkdf2, CryptoUtils } from "@enbox/crypto";
|
|
553
|
+
|
|
554
|
+
// Key Derivation
|
|
555
|
+
const derivedKey = await Pbkdf2.deriveKey({
|
|
556
|
+
hash: "SHA-256", // Hash function to use ('SHA-256', 'SHA-384', 'SHA-512')
|
|
557
|
+
password: new TextEncoder().encode("password"), // Password as a Uint8Array
|
|
558
|
+
salt: CryptoUtils.randomBytes(16), // Salt value
|
|
559
|
+
iterations: 1000, // Number of iterations
|
|
560
|
+
length: 256, // Length of the derived key in bits
|
|
561
|
+
});
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
#### secp256k1
|
|
565
|
+
|
|
566
|
+
```ts
|
|
567
|
+
import { Secp256k1 } from "@enbox/crypto";
|
|
568
|
+
|
|
569
|
+
// Key Generation
|
|
570
|
+
const privateKey = await Secp256k1.generateKey();
|
|
571
|
+
|
|
572
|
+
// Public Key Derivation
|
|
573
|
+
const publicKey = await Secp256k1.computePublicKey({ key: privateKey });
|
|
574
|
+
|
|
575
|
+
// ECDH Shared Secret Computation
|
|
576
|
+
const sharedSecret = await Secp256k1.sharedSecret({
|
|
577
|
+
privateKeyA: privateKey,
|
|
578
|
+
publicKeyB: anotherPublicKey,
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
// ECDSA Signing
|
|
582
|
+
const signature = await Secp256k1.sign({
|
|
583
|
+
key: privateKey,
|
|
584
|
+
data: new TextEncoder().encode("Message"),
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
// ECDSA Signature Verification
|
|
588
|
+
const isValid = await Secp256k1.verify({
|
|
589
|
+
key: publicKey,
|
|
590
|
+
signature: signature,
|
|
591
|
+
data: new TextEncoder().encode("Message"),
|
|
592
|
+
});
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
#### SHA-256
|
|
596
|
+
|
|
597
|
+
```ts
|
|
598
|
+
import { Sha256 } from "@enbox/crypto";
|
|
599
|
+
|
|
600
|
+
// Hashing
|
|
601
|
+
const data = new TextEncoder().encode("Message");
|
|
602
|
+
const hash = await Sha256.digest({ data });
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
#### X25519
|
|
606
|
+
|
|
607
|
+
```ts
|
|
608
|
+
import { X25519 } from "@enbox/crypto";
|
|
609
|
+
|
|
610
|
+
// Key Generation
|
|
611
|
+
const privateKey = await X25519.generateKey();
|
|
612
|
+
|
|
613
|
+
// Public Key Derivation
|
|
614
|
+
const publicKey = await X25519.computePublicKey({ key: privateKey });
|
|
615
|
+
|
|
616
|
+
// ECDH Shared Secret Computation
|
|
617
|
+
const sharedSecret = await X25519.sharedSecret({
|
|
618
|
+
privateKeyA: privateKey,
|
|
619
|
+
publicKeyB: anotherPublicKey,
|
|
620
|
+
});
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
#### XChaCha20
|
|
624
|
+
|
|
625
|
+
```ts
|
|
626
|
+
import { XChaCha20, CryptoUtils } from "@enbox/crypto";
|
|
627
|
+
|
|
628
|
+
// Key Generation
|
|
629
|
+
const privateKey = await XChaCha20.generateKey();
|
|
630
|
+
|
|
631
|
+
// Encryption
|
|
632
|
+
const data = new TextEncoder().encode("Message");
|
|
633
|
+
const nonce = CryptoUtils.randomBytes(24);
|
|
634
|
+
const encryptedData = await XChaCha20.encrypt({
|
|
635
|
+
data,
|
|
636
|
+
nonce,
|
|
637
|
+
key: privateKey,
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// Decryption
|
|
641
|
+
const decryptedData = await XChaCha20.decrypt({
|
|
642
|
+
data: encryptedData,
|
|
643
|
+
nonce,
|
|
644
|
+
key: privateKey,
|
|
645
|
+
});
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
#### XChaCha20-Poly1305
|
|
649
|
+
|
|
650
|
+
```ts
|
|
651
|
+
import { XChaCha20Poly1305, CryptoUtils } from "@enbox/crypto";
|
|
652
|
+
|
|
653
|
+
// Key Generation
|
|
654
|
+
const privateKey = await XChaCha20Poly1305.generateKey();
|
|
655
|
+
|
|
656
|
+
// Encryption
|
|
657
|
+
const data = new TextEncoder().encode("Message");
|
|
658
|
+
const nonce = CryptoUtils.randomBytes(24);
|
|
659
|
+
const additionalData = new TextEncoder().encode("Associated data");
|
|
660
|
+
const { ciphertext, tag } = await XChaCha20Poly1305.encrypt({
|
|
661
|
+
data,
|
|
662
|
+
nonce,
|
|
663
|
+
additionalData,
|
|
664
|
+
key: privateKey,
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
// Decryption
|
|
668
|
+
const decryptedData = await XChaCha20Poly1305.decrypt({
|
|
669
|
+
data: ciphertext,
|
|
670
|
+
nonce,
|
|
671
|
+
tag,
|
|
672
|
+
additionalData,
|
|
673
|
+
key: privateKey,
|
|
674
|
+
});
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
## Project Resources
|
|
678
|
+
|
|
679
|
+
| Resource | Description |
|
|
680
|
+
| --------------------------------------- | ----------------------------------------------------------------------------- |
|
|
681
|
+
| [CODEOWNERS][codeowners-link] | Outlines the project lead(s) |
|
|
682
|
+
| [CODE OF CONDUCT][code-of-conduct-link] | Expected behavior for project contributors, promoting a welcoming environment |
|
|
683
|
+
| [CONTRIBUTING][contributing-link] | Developer guide to build, test, run, access CI, chat, discuss, file issues |
|
|
684
|
+
| [GOVERNANCE][governance-link] | Project governance |
|
|
685
|
+
| [LICENSE][license-link] | Apache License, Version 2.0 |
|
|
686
|
+
|
|
687
|
+
[crypto-npm-badge]: https://img.shields.io/npm/v/@enbox/crypto.svg?style=flat&color=blue&santize=true
|
|
688
|
+
[crypto-npm-link]: https://www.npmjs.com/package/@enbox/crypto
|
|
689
|
+
[crypto-downloads-badge]: https://img.shields.io/npm/dt/@enbox/crypto?&color=blue
|
|
690
|
+
[crypto-build-badge]: https://img.shields.io/github/actions/workflow/status/enboxorg/enbox/enbox/tests-ci.yml?branch=main&label=build
|
|
691
|
+
[crypto-build-link]: https://github.com/enboxorg/enbox/enbox/actions/workflows/tests-ci.yml
|
|
692
|
+
[crypto-coverage-badge]: https://img.shields.io/codecov/c/gh/enboxorg/enbox/enbox/main?style=flat&token=YI87CKF1LI
|
|
693
|
+
[crypto-coverage-link]: https://app.codecov.io/github/enboxorg/enbox/enbox/tree/main/packages%2Fcrypto
|
|
694
|
+
[crypto-issues-badge]: https://img.shields.io/github/issues/enboxorg/enbox/enbox/package:%20crypto?label=issues
|
|
695
|
+
[crypto-issues-link]: https://github.com/enboxorg/enbox/enbox/issues?q=is%3Aopen+is%3Aissue+label%3A"package%3A+crypto"
|
|
696
|
+
[crypto-aws-kms-repo-link]: https://github.com/enboxorg/enbox/enbox/tree/main/packages/crypto-aws-kms
|
|
697
|
+
[crypto-repo-link]: https://github.com/enboxorg/enbox/enbox/tree/main/packages/crypto
|
|
698
|
+
[crypto-jsdelivr-link]: https://www.jsdelivr.com/package/npm/@enbox/crypto
|
|
699
|
+
[crypto-jsdelivr-browser]: https://cdn.jsdelivr.net/npm/@enbox/crypto/dist/browser.mjs
|
|
700
|
+
[crypto-unpkg-link]: https://unpkg.com/@enbox/crypto
|
|
701
|
+
[crypto-unpkg-browser]: https://unpkg.com/@enbox/crypto/dist/browser.mjs
|
|
702
|
+
[codeowners-link]: https://github.com/enboxorg/enbox/enbox/blob/main/CODEOWNERS
|
|
703
|
+
[code-of-conduct-link]: https://github.com/enboxorg/enbox/enbox/blob/main/CODE_OF_CONDUCT.md
|
|
704
|
+
[contributing-link]: https://github.com/enboxorg/enbox/enbox/blob/main/CONTRIBUTING.md
|
|
705
|
+
[governance-link]: https://github.com/enboxorg/enbox/enbox/blob/main/GOVERNANCE.md
|
|
706
|
+
[license-link]: https://github.com/enboxorg/enbox/enbox/blob/main/LICENSE
|
|
707
|
+
[discord-badge]: https://img.shields.io/discord/937858703112155166?color=5865F2&logo=discord&logoColor=white
|
|
708
|
+
[discord-link]: https://discord.com/channels/937858703112155166/969272658501976117
|