@gjsify/crypto 0.3.13 → 0.3.14

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/lib/esm/hmac.js CHANGED
@@ -1,93 +1,98 @@
1
- import { Transform } from "node:stream";
1
+ import { BLOCK_SIZES, SUPPORTED_ALGORITHMS, normalizeAlgorithm } from "./crypto-utils.js";
2
+ import { Hash } from "./hash.js";
2
3
  import { Buffer } from "node:buffer";
4
+ import { Transform } from "node:stream";
3
5
  import { normalizeEncoding } from "@gjsify/utils";
4
- import { Hash } from "./hash.js";
5
- import { normalizeAlgorithm, BLOCK_SIZES, SUPPORTED_ALGORITHMS } from "./crypto-utils.js";
6
- class Hmac extends Transform {
7
- _algorithm;
8
- _innerHash;
9
- _outerKeyPad;
10
- _finalized = false;
11
- constructor(algorithm, key) {
12
- super();
13
- const normalized = normalizeAlgorithm(algorithm);
14
- if (!SUPPORTED_ALGORITHMS.has(normalized)) {
15
- const err = new Error(`Unknown message digest: ${algorithm}`);
16
- err.code = "ERR_CRYPTO_HASH_UNKNOWN";
17
- throw err;
18
- }
19
- this._algorithm = normalized;
20
- let keyBytes;
21
- if (typeof key === "string") {
22
- keyBytes = Buffer.from(key, "utf8");
23
- } else {
24
- keyBytes = key instanceof Uint8Array ? key : Buffer.from(key);
25
- }
26
- const blockSize = BLOCK_SIZES[normalized];
27
- if (keyBytes.length > blockSize) {
28
- const h = new Hash(normalized);
29
- h.update(keyBytes);
30
- keyBytes = h.digest();
31
- }
32
- const paddedKey = new Uint8Array(blockSize);
33
- paddedKey.set(keyBytes);
34
- const iKeyPad = new Uint8Array(blockSize);
35
- const oKeyPad = new Uint8Array(blockSize);
36
- for (let i = 0; i < blockSize; i++) {
37
- iKeyPad[i] = paddedKey[i] ^ 54;
38
- oKeyPad[i] = paddedKey[i] ^ 92;
39
- }
40
- this._outerKeyPad = oKeyPad;
41
- this._innerHash = new Hash(normalized);
42
- this._innerHash.update(iKeyPad);
43
- }
44
- /** Update the HMAC with data. */
45
- update(data, inputEncoding) {
46
- if (this._finalized) {
47
- throw new Error("Digest already called");
48
- }
49
- let bytes;
50
- if (typeof data === "string") {
51
- const enc = normalizeEncoding(inputEncoding);
52
- bytes = Buffer.from(data, enc);
53
- } else {
54
- bytes = data instanceof Uint8Array ? data : Buffer.from(data);
55
- }
56
- this._innerHash.update(bytes);
57
- return this;
58
- }
59
- /** Calculate the HMAC digest. */
60
- digest(encoding) {
61
- if (this._finalized) {
62
- throw new Error("Digest already called");
63
- }
64
- this._finalized = true;
65
- const innerDigest = this._innerHash.digest();
66
- const outerHash = new Hash(this._algorithm);
67
- outerHash.update(this._outerKeyPad);
68
- outerHash.update(innerDigest);
69
- const result = outerHash.digest();
70
- if (encoding) return result.toString(encoding);
71
- return result;
72
- }
73
- // Transform stream interface
74
- _transform(chunk, encoding, callback) {
75
- try {
76
- this.update(chunk, encoding);
77
- callback();
78
- } catch (err) {
79
- callback(err);
80
- }
81
- }
82
- _flush(callback) {
83
- try {
84
- this.push(this.digest());
85
- callback();
86
- } catch (err) {
87
- callback(err);
88
- }
89
- }
90
- }
91
- export {
92
- Hmac
6
+
7
+ //#region src/hmac.ts
8
+ /**
9
+ * Creates and returns an Hmac object that uses the given algorithm and key.
10
+ * Implemented using createHash (GLib.Checksum) since GLib.Hmac bindings are broken in GJS.
11
+ */
12
+ var Hmac = class extends Transform {
13
+ _algorithm;
14
+ _innerHash;
15
+ _outerKeyPad;
16
+ _finalized = false;
17
+ constructor(algorithm, key) {
18
+ super();
19
+ const normalized = normalizeAlgorithm(algorithm);
20
+ if (!SUPPORTED_ALGORITHMS.has(normalized)) {
21
+ const err = new Error(`Unknown message digest: ${algorithm}`);
22
+ err.code = "ERR_CRYPTO_HASH_UNKNOWN";
23
+ throw err;
24
+ }
25
+ this._algorithm = normalized;
26
+ let keyBytes;
27
+ if (typeof key === "string") {
28
+ keyBytes = Buffer.from(key, "utf8");
29
+ } else {
30
+ keyBytes = key instanceof Uint8Array ? key : Buffer.from(key);
31
+ }
32
+ const blockSize = BLOCK_SIZES[normalized];
33
+ if (keyBytes.length > blockSize) {
34
+ const h = new Hash(normalized);
35
+ h.update(keyBytes);
36
+ keyBytes = h.digest();
37
+ }
38
+ const paddedKey = new Uint8Array(blockSize);
39
+ paddedKey.set(keyBytes);
40
+ const iKeyPad = new Uint8Array(blockSize);
41
+ const oKeyPad = new Uint8Array(blockSize);
42
+ for (let i = 0; i < blockSize; i++) {
43
+ iKeyPad[i] = paddedKey[i] ^ 54;
44
+ oKeyPad[i] = paddedKey[i] ^ 92;
45
+ }
46
+ this._outerKeyPad = oKeyPad;
47
+ this._innerHash = new Hash(normalized);
48
+ this._innerHash.update(iKeyPad);
49
+ }
50
+ /** Update the HMAC with data. */
51
+ update(data, inputEncoding) {
52
+ if (this._finalized) {
53
+ throw new Error("Digest already called");
54
+ }
55
+ let bytes;
56
+ if (typeof data === "string") {
57
+ const enc = normalizeEncoding(inputEncoding);
58
+ bytes = Buffer.from(data, enc);
59
+ } else {
60
+ bytes = data instanceof Uint8Array ? data : Buffer.from(data);
61
+ }
62
+ this._innerHash.update(bytes);
63
+ return this;
64
+ }
65
+ /** Calculate the HMAC digest. */
66
+ digest(encoding) {
67
+ if (this._finalized) {
68
+ throw new Error("Digest already called");
69
+ }
70
+ this._finalized = true;
71
+ const innerDigest = this._innerHash.digest();
72
+ const outerHash = new Hash(this._algorithm);
73
+ outerHash.update(this._outerKeyPad);
74
+ outerHash.update(innerDigest);
75
+ const result = outerHash.digest();
76
+ if (encoding) return result.toString(encoding);
77
+ return result;
78
+ }
79
+ _transform(chunk, encoding, callback) {
80
+ try {
81
+ this.update(chunk, encoding);
82
+ callback();
83
+ } catch (err) {
84
+ callback(err);
85
+ }
86
+ }
87
+ _flush(callback) {
88
+ try {
89
+ this.push(this.digest());
90
+ callback();
91
+ } catch (err) {
92
+ callback(err);
93
+ }
94
+ }
93
95
  };
96
+
97
+ //#endregion
98
+ export { Hmac };
package/lib/esm/index.js CHANGED
@@ -1,158 +1,85 @@
1
+ import { createCipher, createCipheriv, createDecipher, createDecipheriv, getCiphers } from "./cipher.js";
2
+ import { constants } from "./constants.js";
3
+ import { randomBytes, randomFill, randomFillSync, randomInt, randomUUID } from "./random.js";
4
+ import { DiffieHellman, DiffieHellmanGroup, createDiffieHellman, createDiffieHellmanGroup, getDiffieHellman } from "./dh.js";
5
+ import { createECDH, getCurves } from "./ecdh.js";
1
6
  import { Hash, getHashes, hash } from "./hash.js";
2
7
  import { Hmac } from "./hmac.js";
3
- import {
4
- randomBytes,
5
- randomFill,
6
- randomFillSync,
7
- randomUUID,
8
- randomInt
9
- } from "./random.js";
8
+ import { ecdsaSign, ecdsaVerify } from "./ecdsa.js";
9
+ import { hkdf, hkdfSync } from "./hkdf.js";
10
10
  import { timingSafeEqual } from "./timing-safe-equal.js";
11
- import { constants } from "./constants.js";
12
11
  import { pbkdf2, pbkdf2Sync } from "./pbkdf2.js";
13
- import { hkdf, hkdfSync } from "./hkdf.js";
14
12
  import { scrypt, scryptSync } from "./scrypt.js";
15
- import { Hash as Hash2 } from "./hash.js";
16
- import { Hmac as Hmac2 } from "./hmac.js";
13
+ import { Sign, Verify, createSign, createVerify } from "./sign.js";
14
+ import { privateDecrypt, privateEncrypt, publicDecrypt, publicEncrypt } from "./public-encrypt.js";
15
+ import { mgf1 } from "./mgf1.js";
16
+ import { rsaPssSign, rsaPssVerify } from "./rsa-pss.js";
17
+ import { rsaOaepDecrypt, rsaOaepEncrypt } from "./rsa-oaep.js";
18
+ import { KeyObject, createPrivateKey, createPublicKey, createSecretKey } from "./key-object.js";
19
+ import { X509Certificate } from "./x509.js";
20
+
21
+ //#region src/index.ts
22
+ /** Create a Hash object for the given algorithm. */
17
23
  function createHash(algorithm) {
18
- return new Hash2(algorithm);
24
+ return new Hash(algorithm);
19
25
  }
26
+ /** Create an Hmac object for the given algorithm and key. */
20
27
  function createHmac(algorithm, key) {
21
- return new Hmac2(algorithm, key);
28
+ return new Hmac(algorithm, key);
22
29
  }
23
- import { createCipher, createCipheriv, createDecipher, createDecipheriv, getCiphers } from "./cipher.js";
24
- import { Sign, Verify, createSign, createVerify } from "./sign.js";
25
- import { createDiffieHellman, getDiffieHellman, DiffieHellman, DiffieHellmanGroup, createDiffieHellmanGroup } from "./dh.js";
26
- import { createECDH, getCurves } from "./ecdh.js";
27
- import { ecdsaSign, ecdsaVerify } from "./ecdsa.js";
28
- import { publicEncrypt, privateDecrypt, privateEncrypt, publicDecrypt } from "./public-encrypt.js";
29
- import { rsaPssSign, rsaPssVerify } from "./rsa-pss.js";
30
- import { rsaOaepEncrypt, rsaOaepDecrypt } from "./rsa-oaep.js";
31
- import { mgf1 } from "./mgf1.js";
32
- import { KeyObject, createSecretKey, createPublicKey, createPrivateKey } from "./key-object.js";
33
- import { X509Certificate } from "./x509.js";
34
- import { getHashes as getHashes2, hash as hash2 } from "./hash.js";
35
- import { randomBytes as randomBytes2, randomFill as randomFill2, randomFillSync as randomFillSync2, randomUUID as randomUUID2, randomInt as randomInt2 } from "./random.js";
36
- import { timingSafeEqual as timingSafeEqual2 } from "./timing-safe-equal.js";
37
- import { constants as constants2 } from "./constants.js";
38
- import { pbkdf2 as pbkdf22, pbkdf2Sync as pbkdf2Sync2 } from "./pbkdf2.js";
39
- import { hkdf as hkdf2, hkdfSync as hkdfSync2 } from "./hkdf.js";
40
- import { scrypt as scrypt2, scryptSync as scryptSync2 } from "./scrypt.js";
41
- import { createCipher as createCipher2, createCipheriv as createCipheriv2, createDecipher as createDecipher2, createDecipheriv as createDecipheriv2, getCiphers as getCiphers2 } from "./cipher.js";
42
- import { Sign as Sign2, Verify as Verify2, createSign as createSign2, createVerify as createVerify2 } from "./sign.js";
43
- import { createDiffieHellman as createDiffieHellman2, getDiffieHellman as getDiffieHellman2, DiffieHellman as DiffieHellman2, DiffieHellmanGroup as DiffieHellmanGroup2, createDiffieHellmanGroup as createDiffieHellmanGroup2 } from "./dh.js";
44
- import { createECDH as createECDH2, getCurves as getCurves2 } from "./ecdh.js";
45
- import { ecdsaSign as ecdsaSign2, ecdsaVerify as ecdsaVerify2 } from "./ecdsa.js";
46
- import { publicEncrypt as publicEncrypt2, privateDecrypt as privateDecrypt2, privateEncrypt as privateEncrypt2, publicDecrypt as publicDecrypt2 } from "./public-encrypt.js";
47
- import { rsaPssSign as rsaPssSign2, rsaPssVerify as rsaPssVerify2 } from "./rsa-pss.js";
48
- import { rsaOaepEncrypt as rsaOaepEncrypt2, rsaOaepDecrypt as rsaOaepDecrypt2 } from "./rsa-oaep.js";
49
- import { mgf1 as mgf12 } from "./mgf1.js";
50
- import { KeyObject as KeyObject2, createSecretKey as createSecretKey2, createPublicKey as createPublicKey2, createPrivateKey as createPrivateKey2 } from "./key-object.js";
51
- import { X509Certificate as X509Certificate2 } from "./x509.js";
52
- var index_default = {
53
- Hash: Hash2,
54
- getHashes: getHashes2,
55
- hash: hash2,
56
- Hmac: Hmac2,
57
- randomBytes: randomBytes2,
58
- randomFill: randomFill2,
59
- randomFillSync: randomFillSync2,
60
- randomUUID: randomUUID2,
61
- randomInt: randomInt2,
62
- timingSafeEqual: timingSafeEqual2,
63
- constants: constants2,
64
- pbkdf2: pbkdf22,
65
- pbkdf2Sync: pbkdf2Sync2,
66
- hkdf: hkdf2,
67
- hkdfSync: hkdfSync2,
68
- scrypt: scrypt2,
69
- scryptSync: scryptSync2,
70
- createHash,
71
- createHmac,
72
- createCipher: createCipher2,
73
- createCipheriv: createCipheriv2,
74
- createDecipher: createDecipher2,
75
- createDecipheriv: createDecipheriv2,
76
- getCiphers: getCiphers2,
77
- Sign: Sign2,
78
- Verify: Verify2,
79
- createSign: createSign2,
80
- createVerify: createVerify2,
81
- createDiffieHellman: createDiffieHellman2,
82
- getDiffieHellman: getDiffieHellman2,
83
- DiffieHellman: DiffieHellman2,
84
- DiffieHellmanGroup: DiffieHellmanGroup2,
85
- createDiffieHellmanGroup: createDiffieHellmanGroup2,
86
- createECDH: createECDH2,
87
- getCurves: getCurves2,
88
- ecdsaSign: ecdsaSign2,
89
- ecdsaVerify: ecdsaVerify2,
90
- publicEncrypt: publicEncrypt2,
91
- privateDecrypt: privateDecrypt2,
92
- privateEncrypt: privateEncrypt2,
93
- publicDecrypt: publicDecrypt2,
94
- rsaPssSign: rsaPssSign2,
95
- rsaPssVerify: rsaPssVerify2,
96
- rsaOaepEncrypt: rsaOaepEncrypt2,
97
- rsaOaepDecrypt: rsaOaepDecrypt2,
98
- mgf1: mgf12,
99
- KeyObject: KeyObject2,
100
- createSecretKey: createSecretKey2,
101
- createPublicKey: createPublicKey2,
102
- createPrivateKey: createPrivateKey2,
103
- X509Certificate: X509Certificate2
104
- };
105
- export {
106
- DiffieHellman,
107
- DiffieHellmanGroup,
108
- Hash,
109
- Hmac,
110
- KeyObject,
111
- Sign,
112
- Verify,
113
- X509Certificate,
114
- constants,
115
- createCipher,
116
- createCipheriv,
117
- createDecipher,
118
- createDecipheriv,
119
- createDiffieHellman,
120
- createDiffieHellmanGroup,
121
- createECDH,
122
- createHash,
123
- createHmac,
124
- createPrivateKey,
125
- createPublicKey,
126
- createSecretKey,
127
- createSign,
128
- createVerify,
129
- index_default as default,
130
- ecdsaSign,
131
- ecdsaVerify,
132
- getCiphers,
133
- getCurves,
134
- getDiffieHellman,
135
- getHashes,
136
- hash,
137
- hkdf,
138
- hkdfSync,
139
- mgf1,
140
- pbkdf2,
141
- pbkdf2Sync,
142
- privateDecrypt,
143
- privateEncrypt,
144
- publicDecrypt,
145
- publicEncrypt,
146
- randomBytes,
147
- randomFill,
148
- randomFillSync,
149
- randomInt,
150
- randomUUID,
151
- rsaOaepDecrypt,
152
- rsaOaepEncrypt,
153
- rsaPssSign,
154
- rsaPssVerify,
155
- scrypt,
156
- scryptSync,
157
- timingSafeEqual
30
+ var src_default = {
31
+ Hash,
32
+ getHashes,
33
+ hash,
34
+ Hmac,
35
+ randomBytes,
36
+ randomFill,
37
+ randomFillSync,
38
+ randomUUID,
39
+ randomInt,
40
+ timingSafeEqual,
41
+ constants,
42
+ pbkdf2,
43
+ pbkdf2Sync,
44
+ hkdf,
45
+ hkdfSync,
46
+ scrypt,
47
+ scryptSync,
48
+ createHash,
49
+ createHmac,
50
+ createCipher,
51
+ createCipheriv,
52
+ createDecipher,
53
+ createDecipheriv,
54
+ getCiphers,
55
+ Sign,
56
+ Verify,
57
+ createSign,
58
+ createVerify,
59
+ createDiffieHellman,
60
+ getDiffieHellman,
61
+ DiffieHellman,
62
+ DiffieHellmanGroup,
63
+ createDiffieHellmanGroup,
64
+ createECDH,
65
+ getCurves,
66
+ ecdsaSign,
67
+ ecdsaVerify,
68
+ publicEncrypt,
69
+ privateDecrypt,
70
+ privateEncrypt,
71
+ publicDecrypt,
72
+ rsaPssSign,
73
+ rsaPssVerify,
74
+ rsaOaepEncrypt,
75
+ rsaOaepDecrypt,
76
+ mgf1,
77
+ KeyObject,
78
+ createSecretKey,
79
+ createPublicKey,
80
+ createPrivateKey,
81
+ X509Certificate
158
82
  };
83
+
84
+ //#endregion
85
+ export { DiffieHellman, DiffieHellmanGroup, Hash, Hmac, KeyObject, Sign, Verify, X509Certificate, constants, createCipher, createCipheriv, createDecipher, createDecipheriv, createDiffieHellman, createDiffieHellmanGroup, createECDH, createHash, createHmac, createPrivateKey, createPublicKey, createSecretKey, createSign, createVerify, src_default as default, ecdsaSign, ecdsaVerify, getCiphers, getCurves, getDiffieHellman, getHashes, hash, hkdf, hkdfSync, mgf1, pbkdf2, pbkdf2Sync, privateDecrypt, privateEncrypt, publicDecrypt, publicEncrypt, randomBytes, randomFill, randomFillSync, randomInt, randomUUID, rsaOaepDecrypt, rsaOaepEncrypt, rsaPssSign, rsaPssVerify, scrypt, scryptSync, timingSafeEqual };