@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.
Files changed (87) hide show
  1. package/README.md +27 -0
  2. package/lib/esm/asn1.js +504 -0
  3. package/lib/esm/bigint-math.js +34 -0
  4. package/lib/esm/cipher.js +1272 -0
  5. package/lib/esm/constants.js +15 -0
  6. package/lib/esm/crypto-utils.js +47 -0
  7. package/lib/esm/dh.js +411 -0
  8. package/lib/esm/ecdh.js +356 -0
  9. package/lib/esm/ecdsa.js +125 -0
  10. package/lib/esm/hash.js +100 -0
  11. package/lib/esm/hkdf.js +58 -0
  12. package/lib/esm/hmac.js +93 -0
  13. package/lib/esm/index.js +158 -0
  14. package/lib/esm/key-object.js +330 -0
  15. package/lib/esm/mgf1.js +27 -0
  16. package/lib/esm/pbkdf2.js +68 -0
  17. package/lib/esm/public-encrypt.js +175 -0
  18. package/lib/esm/random.js +138 -0
  19. package/lib/esm/rsa-oaep.js +95 -0
  20. package/lib/esm/rsa-pss.js +100 -0
  21. package/lib/esm/scrypt.js +134 -0
  22. package/lib/esm/sign.js +248 -0
  23. package/lib/esm/timing-safe-equal.js +13 -0
  24. package/lib/esm/x509.js +214 -0
  25. package/lib/types/asn1.d.ts +87 -0
  26. package/lib/types/bigint-math.d.ts +13 -0
  27. package/lib/types/cipher.d.ts +84 -0
  28. package/lib/types/constants.d.ts +10 -0
  29. package/lib/types/crypto-utils.d.ts +22 -0
  30. package/lib/types/dh.d.ts +79 -0
  31. package/lib/types/ecdh.d.ts +96 -0
  32. package/lib/types/ecdsa.d.ts +21 -0
  33. package/lib/types/hash.d.ts +25 -0
  34. package/lib/types/hkdf.d.ts +9 -0
  35. package/lib/types/hmac.d.ts +20 -0
  36. package/lib/types/index.d.ts +105 -0
  37. package/lib/types/key-object.d.ts +36 -0
  38. package/lib/types/mgf1.d.ts +5 -0
  39. package/lib/types/pbkdf2.d.ts +9 -0
  40. package/lib/types/public-encrypt.d.ts +42 -0
  41. package/lib/types/random.d.ts +22 -0
  42. package/lib/types/rsa-oaep.d.ts +8 -0
  43. package/lib/types/rsa-pss.d.ts +8 -0
  44. package/lib/types/scrypt.d.ts +11 -0
  45. package/lib/types/sign.d.ts +61 -0
  46. package/lib/types/timing-safe-equal.d.ts +6 -0
  47. package/lib/types/x509.d.ts +72 -0
  48. package/package.json +45 -0
  49. package/src/asn1.ts +797 -0
  50. package/src/bigint-math.ts +45 -0
  51. package/src/cipher.spec.ts +332 -0
  52. package/src/cipher.ts +952 -0
  53. package/src/constants.ts +16 -0
  54. package/src/crypto-utils.ts +64 -0
  55. package/src/dh.spec.ts +111 -0
  56. package/src/dh.ts +761 -0
  57. package/src/ecdh.spec.ts +116 -0
  58. package/src/ecdh.ts +624 -0
  59. package/src/ecdsa.ts +243 -0
  60. package/src/extended.spec.ts +444 -0
  61. package/src/gcm.spec.ts +141 -0
  62. package/src/hash.spec.ts +86 -0
  63. package/src/hash.ts +119 -0
  64. package/src/hkdf.ts +99 -0
  65. package/src/hmac.spec.ts +64 -0
  66. package/src/hmac.ts +123 -0
  67. package/src/index.ts +93 -0
  68. package/src/key-object.spec.ts +202 -0
  69. package/src/key-object.ts +401 -0
  70. package/src/mgf1.ts +37 -0
  71. package/src/pbkdf2.spec.ts +76 -0
  72. package/src/pbkdf2.ts +106 -0
  73. package/src/public-encrypt.ts +288 -0
  74. package/src/random.spec.ts +133 -0
  75. package/src/random.ts +183 -0
  76. package/src/rsa-oaep.ts +167 -0
  77. package/src/rsa-pss.ts +190 -0
  78. package/src/scrypt.spec.ts +90 -0
  79. package/src/scrypt.ts +191 -0
  80. package/src/sign.spec.ts +160 -0
  81. package/src/sign.ts +319 -0
  82. package/src/test.mts +19 -0
  83. package/src/timing-safe-equal.ts +21 -0
  84. package/src/x509.spec.ts +210 -0
  85. package/src/x509.ts +262 -0
  86. package/tsconfig.json +31 -0
  87. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,16 @@
1
+ // Reference: Node.js lib/internal/crypto/util.js — crypto constants
2
+ // Subset of Node.js crypto constants relevant to the GJS implementation
3
+
4
+ export const constants = {
5
+ // RSA padding modes (stubs for compatibility)
6
+ RSA_PKCS1_PADDING: 1,
7
+ RSA_NO_PADDING: 3,
8
+ RSA_PKCS1_OAEP_PADDING: 4,
9
+ RSA_PKCS1_PSS_PADDING: 6,
10
+
11
+ // DiffieHellman error codes
12
+ DH_CHECK_P_NOT_SAFE_PRIME: 2,
13
+ DH_CHECK_P_NOT_PRIME: 1,
14
+ DH_UNABLE_TO_CHECK_GENERATOR: 4,
15
+ DH_NOT_SUITABLE_GENERATOR: 8,
16
+ } as const;
@@ -0,0 +1,64 @@
1
+ // Crypto utilities for GJS — original implementation
2
+ // Centralizes algorithm normalization, digest/block sizes, and common helpers
3
+ // used across hash, hmac, pbkdf2, hkdf, mgf1, rsa-pss, rsa-oaep, ecdsa, and sign modules.
4
+
5
+ import { Buffer } from 'node:buffer';
6
+
7
+ /**
8
+ * Normalize a hash algorithm name to lowercase without hyphens.
9
+ * e.g. "SHA-256" → "sha256", "MD5" → "md5"
10
+ */
11
+ export function normalizeAlgorithm(algorithm: string): string {
12
+ return algorithm.toLowerCase().replace(/-/g, '');
13
+ }
14
+
15
+ /** Hash digest output sizes in bytes. */
16
+ export const DIGEST_SIZES: Record<string, number> = {
17
+ md5: 16,
18
+ sha1: 20,
19
+ sha256: 32,
20
+ sha384: 48,
21
+ sha512: 64,
22
+ };
23
+
24
+ /** Hash block sizes in bytes (used for HMAC key padding). */
25
+ export const BLOCK_SIZES: Record<string, number> = {
26
+ md5: 64,
27
+ sha1: 64,
28
+ sha256: 64,
29
+ sha384: 128,
30
+ sha512: 128,
31
+ };
32
+
33
+ /** Set of supported hash algorithm names (normalized). */
34
+ export const SUPPORTED_ALGORITHMS = new Set(['md5', 'sha1', 'sha256', 'sha384', 'sha512']);
35
+
36
+ /**
37
+ * Get hash digest size in bytes for a given algorithm.
38
+ * Accepts unnormalized names (e.g. "SHA-256").
39
+ */
40
+ export function hashSize(algo: string): number {
41
+ const normalized = normalizeAlgorithm(algo);
42
+ const size = DIGEST_SIZES[normalized];
43
+ if (size === undefined) {
44
+ throw new Error(`Unknown hash algorithm: ${algo}`);
45
+ }
46
+ return size;
47
+ }
48
+
49
+ /**
50
+ * Convert various input types to Buffer.
51
+ * Handles string, Buffer, Uint8Array, DataView, and ArrayBuffer.
52
+ */
53
+ export function toBuffer(input: string | Buffer | Uint8Array | DataView | ArrayBuffer, encoding?: string): Buffer {
54
+ if (typeof input === 'string') {
55
+ return Buffer.from(input, (encoding as BufferEncoding) || 'utf8');
56
+ }
57
+ if (input instanceof DataView) {
58
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
59
+ }
60
+ if (input instanceof ArrayBuffer) {
61
+ return Buffer.from(input);
62
+ }
63
+ return Buffer.from(input);
64
+ }
package/src/dh.spec.ts ADDED
@@ -0,0 +1,111 @@
1
+ // Ported from refs/node-test/parallel/test-crypto-dh.js
2
+ // Original: MIT license, Copyright (c) Node.js contributors
3
+
4
+ import { describe, it, expect } from '@gjsify/unit';
5
+ import { createDiffieHellman, getDiffieHellman } from 'node:crypto';
6
+ import { Buffer } from 'node:buffer';
7
+
8
+ export default async () => {
9
+
10
+ await describe('crypto.createDiffieHellman', async () => {
11
+ await it('should be a function', async () => {
12
+ expect(typeof createDiffieHellman).toBe('function');
13
+ });
14
+
15
+ await it('should create DH with prime buffer', async () => {
16
+ // Use a predefined group prime instead of generating (Node.js rejects small primes)
17
+ const dh = getDiffieHellman('modp14');
18
+ expect(dh).toBeDefined();
19
+ expect(typeof dh.generateKeys).toBe('function');
20
+ });
21
+
22
+ await it('should generate keys', async () => {
23
+ const dh = getDiffieHellman('modp14');
24
+ const keys = dh.generateKeys();
25
+ expect(keys).toBeDefined();
26
+ expect(Buffer.isBuffer(keys)).toBe(true);
27
+ expect(keys.length).toBeGreaterThan(0);
28
+ });
29
+
30
+ await it('should get prime and generator', async () => {
31
+ const dh = getDiffieHellman('modp14');
32
+ dh.generateKeys();
33
+ const prime = dh.getPrime();
34
+ const generator = dh.getGenerator();
35
+ expect(Buffer.isBuffer(prime)).toBe(true);
36
+ expect(Buffer.isBuffer(generator)).toBe(true);
37
+ expect(prime.length).toBeGreaterThan(0);
38
+ expect(generator.length).toBeGreaterThan(0);
39
+ });
40
+
41
+ await it('should compute shared secret', async () => {
42
+ const alice = createDiffieHellman(512);
43
+ alice.generateKeys();
44
+
45
+ const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
46
+ bob.generateKeys();
47
+
48
+ const aliceSecret = alice.computeSecret(bob.getPublicKey());
49
+ const bobSecret = bob.computeSecret(alice.getPublicKey());
50
+
51
+ expect(aliceSecret.toString('hex')).toBe(bobSecret.toString('hex'));
52
+ });
53
+
54
+ await it('should support hex encoding for keys', async () => {
55
+ const dh = getDiffieHellman('modp5');
56
+ dh.generateKeys();
57
+
58
+ const pubHex = dh.getPublicKey('hex');
59
+ const privHex = dh.getPrivateKey('hex');
60
+ expect(typeof pubHex).toBe('string');
61
+ expect(typeof privHex).toBe('string');
62
+ expect(pubHex.length).toBeGreaterThan(0);
63
+ expect(privHex.length).toBeGreaterThan(0);
64
+ });
65
+
66
+ await it('should create DH from prime buffer', async () => {
67
+ const source = getDiffieHellman('modp5');
68
+ const primeBuf = source.getPrime();
69
+
70
+ const dh = createDiffieHellman(primeBuf);
71
+ dh.generateKeys();
72
+ expect(dh.getPrime('hex')).toBe(primeBuf.toString('hex'));
73
+ });
74
+ });
75
+
76
+ await describe('crypto.getDiffieHellman', async () => {
77
+ await it('should be a function', async () => {
78
+ expect(typeof getDiffieHellman).toBe('function');
79
+ });
80
+
81
+ await it('should return a DH object for modp14', async () => {
82
+ const dh = getDiffieHellman('modp14');
83
+ expect(dh).toBeDefined();
84
+ expect(typeof dh.generateKeys).toBe('function');
85
+ });
86
+
87
+ await it('should compute shared secret with predefined group', async () => {
88
+ const alice = getDiffieHellman('modp14');
89
+ alice.generateKeys();
90
+
91
+ const bob = getDiffieHellman('modp14');
92
+ bob.generateKeys();
93
+
94
+ const aliceSecret = alice.computeSecret(bob.getPublicKey());
95
+ const bobSecret = bob.computeSecret(alice.getPublicKey());
96
+
97
+ expect(aliceSecret.toString('hex')).toBe(bobSecret.toString('hex'));
98
+ });
99
+
100
+ await it('should support modp5', async () => {
101
+ const dh = getDiffieHellman('modp5');
102
+ dh.generateKeys();
103
+ const pub = dh.getPublicKey();
104
+ expect(pub.length).toBeGreaterThan(0);
105
+ });
106
+
107
+ await it('should throw for unknown group', async () => {
108
+ expect(() => getDiffieHellman('modp999')).toThrow();
109
+ });
110
+ });
111
+ };