@gjsify/crypto 0.1.0
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/README.md +27 -0
- package/lib/esm/asn1.js +504 -0
- package/lib/esm/bigint-math.js +34 -0
- package/lib/esm/cipher.js +1272 -0
- package/lib/esm/constants.js +15 -0
- package/lib/esm/crypto-utils.js +47 -0
- package/lib/esm/dh.js +411 -0
- package/lib/esm/ecdh.js +356 -0
- package/lib/esm/ecdsa.js +125 -0
- package/lib/esm/hash.js +100 -0
- package/lib/esm/hkdf.js +58 -0
- package/lib/esm/hmac.js +93 -0
- package/lib/esm/index.js +158 -0
- package/lib/esm/key-object.js +330 -0
- package/lib/esm/mgf1.js +27 -0
- package/lib/esm/pbkdf2.js +68 -0
- package/lib/esm/public-encrypt.js +175 -0
- package/lib/esm/random.js +138 -0
- package/lib/esm/rsa-oaep.js +95 -0
- package/lib/esm/rsa-pss.js +100 -0
- package/lib/esm/scrypt.js +134 -0
- package/lib/esm/sign.js +248 -0
- package/lib/esm/timing-safe-equal.js +13 -0
- package/lib/esm/x509.js +214 -0
- package/lib/types/asn1.d.ts +87 -0
- package/lib/types/bigint-math.d.ts +13 -0
- package/lib/types/cipher.d.ts +84 -0
- package/lib/types/constants.d.ts +10 -0
- package/lib/types/crypto-utils.d.ts +22 -0
- package/lib/types/dh.d.ts +79 -0
- package/lib/types/ecdh.d.ts +96 -0
- package/lib/types/ecdsa.d.ts +21 -0
- package/lib/types/hash.d.ts +25 -0
- package/lib/types/hkdf.d.ts +9 -0
- package/lib/types/hmac.d.ts +20 -0
- package/lib/types/index.d.ts +105 -0
- package/lib/types/key-object.d.ts +36 -0
- package/lib/types/mgf1.d.ts +5 -0
- package/lib/types/pbkdf2.d.ts +9 -0
- package/lib/types/public-encrypt.d.ts +42 -0
- package/lib/types/random.d.ts +22 -0
- package/lib/types/rsa-oaep.d.ts +8 -0
- package/lib/types/rsa-pss.d.ts +8 -0
- package/lib/types/scrypt.d.ts +11 -0
- package/lib/types/sign.d.ts +61 -0
- package/lib/types/timing-safe-equal.d.ts +6 -0
- package/lib/types/x509.d.ts +72 -0
- package/package.json +45 -0
- package/src/asn1.ts +797 -0
- package/src/bigint-math.ts +45 -0
- package/src/cipher.spec.ts +332 -0
- package/src/cipher.ts +952 -0
- package/src/constants.ts +16 -0
- package/src/crypto-utils.ts +64 -0
- package/src/dh.spec.ts +111 -0
- package/src/dh.ts +761 -0
- package/src/ecdh.spec.ts +116 -0
- package/src/ecdh.ts +624 -0
- package/src/ecdsa.ts +243 -0
- package/src/extended.spec.ts +444 -0
- package/src/gcm.spec.ts +141 -0
- package/src/hash.spec.ts +86 -0
- package/src/hash.ts +119 -0
- package/src/hkdf.ts +99 -0
- package/src/hmac.spec.ts +64 -0
- package/src/hmac.ts +123 -0
- package/src/index.ts +93 -0
- package/src/key-object.spec.ts +202 -0
- package/src/key-object.ts +401 -0
- package/src/mgf1.ts +37 -0
- package/src/pbkdf2.spec.ts +76 -0
- package/src/pbkdf2.ts +106 -0
- package/src/public-encrypt.ts +288 -0
- package/src/random.spec.ts +133 -0
- package/src/random.ts +183 -0
- package/src/rsa-oaep.ts +167 -0
- package/src/rsa-pss.ts +190 -0
- package/src/scrypt.spec.ts +90 -0
- package/src/scrypt.ts +191 -0
- package/src/sign.spec.ts +160 -0
- package/src/sign.ts +319 -0
- package/src/test.mts +19 -0
- package/src/timing-safe-equal.ts +21 -0
- package/src/x509.spec.ts +210 -0
- package/src/x509.ts +262 -0
- package/tsconfig.json +31 -0
- package/tsconfig.tsbuildinfo +1 -0
package/src/ecdh.spec.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Ported from refs/node-test/parallel/test-crypto-ecdh.js
|
|
2
|
+
// Original: MIT license, Copyright (c) Node.js contributors
|
|
3
|
+
|
|
4
|
+
import { describe, it, expect } from '@gjsify/unit';
|
|
5
|
+
import { createECDH, getCurves } from 'node:crypto';
|
|
6
|
+
import { Buffer } from 'node:buffer';
|
|
7
|
+
|
|
8
|
+
export default async () => {
|
|
9
|
+
|
|
10
|
+
await describe('crypto.getCurves', async () => {
|
|
11
|
+
await it('should return an array', async () => {
|
|
12
|
+
const curves = getCurves();
|
|
13
|
+
expect(Array.isArray(curves)).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
await it('should contain common curves', async () => {
|
|
17
|
+
const curves = getCurves();
|
|
18
|
+
expect(curves).toContain('secp256k1');
|
|
19
|
+
expect(curves).toContain('prime256v1');
|
|
20
|
+
expect(curves).toContain('secp384r1');
|
|
21
|
+
expect(curves).toContain('secp521r1');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
await describe('crypto.createECDH', async () => {
|
|
26
|
+
await it('should be a function', async () => {
|
|
27
|
+
expect(typeof createECDH).toBe('function');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await it('should create an ECDH instance', async () => {
|
|
31
|
+
const ecdh = createECDH('secp256k1');
|
|
32
|
+
expect(ecdh).toBeDefined();
|
|
33
|
+
expect(typeof ecdh.generateKeys).toBe('function');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await it('should generate keys for secp256k1', async () => {
|
|
37
|
+
const ecdh = createECDH('secp256k1');
|
|
38
|
+
const pub = ecdh.generateKeys();
|
|
39
|
+
expect(Buffer.isBuffer(pub)).toBe(true);
|
|
40
|
+
// Uncompressed public key: 0x04 + 32 bytes X + 32 bytes Y = 65 bytes
|
|
41
|
+
expect(pub.length).toBe(65);
|
|
42
|
+
expect(pub[0]).toBe(0x04);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
await it('should generate keys for prime256v1', async () => {
|
|
46
|
+
const ecdh = createECDH('prime256v1');
|
|
47
|
+
const pub = ecdh.generateKeys();
|
|
48
|
+
expect(Buffer.isBuffer(pub)).toBe(true);
|
|
49
|
+
expect(pub.length).toBe(65);
|
|
50
|
+
expect(pub[0]).toBe(0x04);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await it('should compute shared secret for secp256k1', async () => {
|
|
54
|
+
const alice = createECDH('secp256k1');
|
|
55
|
+
alice.generateKeys();
|
|
56
|
+
|
|
57
|
+
const bob = createECDH('secp256k1');
|
|
58
|
+
bob.generateKeys();
|
|
59
|
+
|
|
60
|
+
const aliceSecret = alice.computeSecret(bob.getPublicKey());
|
|
61
|
+
const bobSecret = bob.computeSecret(alice.getPublicKey());
|
|
62
|
+
|
|
63
|
+
expect(aliceSecret.toString('hex')).toBe(bobSecret.toString('hex'));
|
|
64
|
+
expect(aliceSecret.length).toBe(32);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await it('should compute shared secret for prime256v1', async () => {
|
|
68
|
+
const alice = createECDH('prime256v1');
|
|
69
|
+
alice.generateKeys();
|
|
70
|
+
|
|
71
|
+
const bob = createECDH('prime256v1');
|
|
72
|
+
bob.generateKeys();
|
|
73
|
+
|
|
74
|
+
const aliceSecret = alice.computeSecret(bob.getPublicKey());
|
|
75
|
+
const bobSecret = bob.computeSecret(alice.getPublicKey());
|
|
76
|
+
|
|
77
|
+
expect(aliceSecret.toString('hex')).toBe(bobSecret.toString('hex'));
|
|
78
|
+
expect(aliceSecret.length).toBe(32);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await it('should get/set private key', async () => {
|
|
82
|
+
const ecdh = createECDH('secp256k1');
|
|
83
|
+
ecdh.generateKeys();
|
|
84
|
+
const priv = ecdh.getPrivateKey();
|
|
85
|
+
expect(Buffer.isBuffer(priv)).toBe(true);
|
|
86
|
+
expect(priv.length).toBe(32);
|
|
87
|
+
|
|
88
|
+
const ecdh2 = createECDH('secp256k1');
|
|
89
|
+
ecdh2.setPrivateKey(priv);
|
|
90
|
+
expect(ecdh2.getPrivateKey('hex')).toBe(priv.toString('hex'));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await it('should support compressed public key format', async () => {
|
|
94
|
+
const ecdh = createECDH('secp256k1');
|
|
95
|
+
ecdh.generateKeys();
|
|
96
|
+
|
|
97
|
+
const compressed = ecdh.getPublicKey(null, 'compressed');
|
|
98
|
+
expect(Buffer.isBuffer(compressed)).toBe(true);
|
|
99
|
+
expect(compressed.length).toBe(33);
|
|
100
|
+
expect(compressed[0] === 0x02 || compressed[0] === 0x03).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await it('should support hex encoding', async () => {
|
|
104
|
+
const ecdh = createECDH('secp256k1');
|
|
105
|
+
ecdh.generateKeys();
|
|
106
|
+
|
|
107
|
+
const pubHex = ecdh.getPublicKey('hex');
|
|
108
|
+
expect(typeof pubHex).toBe('string');
|
|
109
|
+
expect(pubHex.length).toBe(130); // 65 bytes * 2 hex chars
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await it('should throw for unknown curve', async () => {
|
|
113
|
+
expect(() => createECDH('nonexistent-curve')).toThrow();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
};
|