@1auth/crypto 0.0.0-alpha.9 → 0.0.0-beta.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/index.js +730 -368
- package/package.json +53 -53
- package/LICENSE +0 -21
package/index.js
CHANGED
|
@@ -1,30 +1,123 @@
|
|
|
1
|
-
import { promisify } from 'node:util'
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
argon2Sync,
|
|
3
|
+
createCipheriv,
|
|
4
|
+
createDecipheriv,
|
|
5
|
+
createHash,
|
|
6
|
+
createHmac,
|
|
7
|
+
generateKeyPair as generateKeyPairCallback,
|
|
8
|
+
randomBytes,
|
|
9
|
+
randomInt,
|
|
10
|
+
sign as signCallback,
|
|
11
|
+
timingSafeEqual,
|
|
12
|
+
verify as verifyCallback,
|
|
13
|
+
} from "node:crypto";
|
|
14
|
+
import { promisify } from "node:util";
|
|
15
|
+
import { customAlphabet } from "nanoid";
|
|
16
|
+
|
|
17
|
+
const generateKeyPair = promisify(generateKeyPairCallback);
|
|
18
|
+
const sign = promisify(signCallback);
|
|
19
|
+
const verify = promisify(verifyCallback);
|
|
20
|
+
|
|
21
|
+
const defaults = {
|
|
22
|
+
symmetricEncryptionKey: undefined, // symmetricRandomEncryptionKey()
|
|
23
|
+
symmetricEncryptionAlgorithm: "chacha20-poly1305", // 2025-03: AES-256 GCM (aes-256-gcm) or ChaCha20-Poly1305 (chacha20-poly1305)
|
|
24
|
+
symmetricEncryptionEncoding: undefined, // https://nodejs.org/api/buffer.html#buffers-and-character-encodings
|
|
25
|
+
symmetricSignatureHashAlgorithm: undefined, // fallback to defaultHashAlgorithm
|
|
26
|
+
symmetricSignatureSecret: undefined, // symmetricRandomSignatureSecret()
|
|
27
|
+
symmetricSignatureEncoding: undefined, // fallback to defaultEncoding
|
|
28
|
+
asymmetricKeyNamedCurve: "P-384", // P-512
|
|
29
|
+
asymmetricSignatureHashAlgorithm: undefined, // fallback to defaultHashAlgorithm
|
|
30
|
+
asymmetricSignatureEncoding: undefined, // fallback to defaultEncoding
|
|
31
|
+
digestChecksumHashAlgorithm: undefined, // fallback to defaultHashAlgorithm
|
|
32
|
+
digestChecksumEncoding: undefined,
|
|
33
|
+
digestChecksumSalt: undefined, // randomChecksumSalt()
|
|
34
|
+
digestChecksumPepper: undefined, // randomChecksumPepper()
|
|
35
|
+
secretArgon2Algorithm: "argon2id",
|
|
36
|
+
secretArgon2Version: 19, // argon2id
|
|
37
|
+
secretArgon2Parallelism: 1, // argon2id
|
|
38
|
+
secretArgon2MemoryCost: 15, // argon2id
|
|
39
|
+
secretArgon2TimeCost: 3, // argon2id
|
|
40
|
+
secretArgon2NonceLength: 16, // argon2id
|
|
41
|
+
secretArgon2HashLength: 64, // argon2id
|
|
42
|
+
|
|
43
|
+
defaultEncoding: "base64",
|
|
44
|
+
defaultHashAlgorithm: "sha3-384",
|
|
45
|
+
};
|
|
46
|
+
const symmetricEncryptionEncodingLengths = {};
|
|
47
|
+
const options = {};
|
|
48
|
+
export default (opt = {}) => {
|
|
49
|
+
Object.assign(options, defaults, opt);
|
|
50
|
+
|
|
51
|
+
// Check options, set defaults
|
|
52
|
+
if (!options.symmetricEncryptionKey) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"@1auth/crypto symmetricEncryptionKey is empty, use a stored secret made from randomBytes(32) Encryption disabled.",
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
options.symmetricEncryptionKey = makeOptionsBuffer(
|
|
58
|
+
options.symmetricEncryptionKey,
|
|
59
|
+
);
|
|
60
|
+
options.symmetricEncryptionEncoding ??= options.defaultEncoding;
|
|
61
|
+
options.symmetricSignatureHashAlgorithm ??= options.defaultHashAlgorithm;
|
|
62
|
+
if (!options.symmetricSignatureSecret) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"@1auth/crypto symmetricSignatureSecret is empty, use a stored secret made from randomBytes(32) Signature disabled.",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
options.symmetricSignatureSecret = makeOptionsBuffer(
|
|
68
|
+
options.symmetricSignatureSecret,
|
|
69
|
+
);
|
|
70
|
+
options.symmetricSignatureEncoding ??= options.defaultEncoding;
|
|
71
|
+
options.asymmetricSignatureHashAlgorithm ??= options.defaultHashAlgorithm;
|
|
72
|
+
options.asymmetricSignatureEncoding ??= options.defaultEncoding;
|
|
73
|
+
if (!options.digestChecksumSalt) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"@1auth/crypto digestChecksumSalt is empty, use a stored secret made from randomBytes(32) Checksum salting disabled.",
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
options.digestChecksumSalt = makeOptionsBuffer(options.digestChecksumSalt);
|
|
79
|
+
if (!options.digestChecksumPepper) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"@1auth/crypto digestChecksumPepper is empty, use a stored secret made from randomBytes(12) Checksum peppering disabled.",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
options.digestChecksumPepper = makeOptionsBuffer(
|
|
85
|
+
options.digestChecksumPepper,
|
|
86
|
+
);
|
|
87
|
+
options.digestChecksumHashAlgorithm ??= options.defaultHashAlgorithm;
|
|
88
|
+
options.digestChecksumEncoding ??= options.defaultEncoding;
|
|
89
|
+
|
|
90
|
+
// Secrets
|
|
91
|
+
Object.assign(argon2Options, {
|
|
92
|
+
algorithm: options.secretArgon2Algorithm,
|
|
93
|
+
version: options.secretArgon2Version, // argon2id
|
|
94
|
+
parallelism: options.secretArgon2Parallelism, // argon2id
|
|
95
|
+
memoryCost: options.secretArgon2MemoryCost, // argon2id
|
|
96
|
+
timeCost: options.secretArgon2TimeCost, // argon2id
|
|
97
|
+
nonceLength: options.secretArgon2NonceLength, // argon2id
|
|
98
|
+
hashLength: options.secretArgon2HashLength, // argon2id
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Lengths
|
|
102
|
+
symmetricEncryptionEncodingLengths.iv = randomIV().toString(
|
|
103
|
+
options.symmetricEncryptionEncoding,
|
|
104
|
+
).length;
|
|
105
|
+
symmetricEncryptionEncodingLengths.ivAndAuthTag =
|
|
106
|
+
symmetricEncryptionEncodingLengths.iv +
|
|
107
|
+
randomBytes(16).toString(options.symmetricEncryptionEncoding).length;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const makeOptionsBuffer = (
|
|
111
|
+
value,
|
|
112
|
+
encoding = options.defaultEncoding,
|
|
113
|
+
) => {
|
|
114
|
+
if (typeof value === "string") {
|
|
115
|
+
return Buffer.from(value, encoding);
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const getOptions = () => options;
|
|
28
121
|
|
|
29
122
|
// *** entropy *** //
|
|
30
123
|
// export const characterPoolSize = (value) => {
|
|
@@ -42,359 +135,628 @@ const generateKeyPair = promisify(generateKeyPairCallback)
|
|
|
42
135
|
// return max - min
|
|
43
136
|
// }
|
|
44
137
|
|
|
45
|
-
// *** Random generators *** //
|
|
46
|
-
export const characterPoolSize = {
|
|
47
|
-
keyboard: 94, // (26 + 10 + 11) * 2
|
|
48
|
-
alphaNumeric: 62, // (26 + 10) * 2
|
|
49
|
-
base64: 64, // (26 + 10) * 2 + 2
|
|
50
|
-
hex: 16,
|
|
51
|
-
numeric: 10
|
|
52
|
-
}
|
|
53
|
-
// Alt: https://github.com/sindresorhus/crypto-random-string/blob/main/core.js
|
|
54
|
-
export const randomAlphaNumeric = (bits) => {
|
|
55
|
-
const characterLength = entropyToCharacterLength(
|
|
56
|
-
bits,
|
|
57
|
-
characterPoolSize.alphaNumeric
|
|
58
|
-
)
|
|
59
|
-
return randomBytes(characterLength * 2)
|
|
60
|
-
.toString('base64')
|
|
61
|
-
.replace(/[^a-zA-Z0-9]/g, '')
|
|
62
|
-
.substring(0, characterLength)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const randomBase64 = (bits) => {
|
|
66
|
-
const characterLength = entropyToCharacterLength(
|
|
67
|
-
bits,
|
|
68
|
-
characterPoolSize.base64
|
|
69
|
-
)
|
|
70
|
-
return randomBytes(characterLength + 1)
|
|
71
|
-
.toString('base64')
|
|
72
|
-
.replace('=', '')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export const randomNumeric = (bits) => {
|
|
76
|
-
const characterLength = entropyToCharacterLength(
|
|
77
|
-
bits,
|
|
78
|
-
characterPoolSize.numeric
|
|
79
|
-
)
|
|
80
|
-
const max = 10 ** characterLength
|
|
81
|
-
let value = Math.floor(Math.random() * max).toString()
|
|
82
|
-
if (value === max.toString()) {
|
|
83
|
-
return randomNumeric(bits)
|
|
84
|
-
}
|
|
85
|
-
while (value.length < characterLength) value = '0' + value
|
|
86
|
-
return value
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const randomHex = (bits) => {
|
|
90
|
-
const characterLength = entropyToCharacterLength(bits, characterPoolSize.hex)
|
|
91
|
-
return randomBytes(characterLength).toString('hex')
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// *** configs *** //
|
|
95
|
-
export const randomId = {
|
|
96
|
-
type: 'id',
|
|
97
|
-
entropy: 64,
|
|
98
|
-
charPool: characterPoolSize.alphaNumeric,
|
|
99
|
-
create: async () => randomAlphaNumeric(randomId.entropy)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export const subject = {
|
|
103
|
-
type: 'id',
|
|
104
|
-
entropy: 64,
|
|
105
|
-
charPool: characterPoolSize.alphaNumeric,
|
|
106
|
-
create: async () => randomAlphaNumeric(subject.entropy)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export const session = {
|
|
110
|
-
type: 'id',
|
|
111
|
-
entropy: 128, // ASVS 3.2.2
|
|
112
|
-
charPool: characterPoolSize.alphaNumeric,
|
|
113
|
-
expire: 15 * 60,
|
|
114
|
-
create: async () => randomAlphaNumeric(session.entropy)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export const passwordSecret = {
|
|
118
|
-
type: 'secret',
|
|
119
|
-
entropy: 64,
|
|
120
|
-
charPool: characterPoolSize.keyboard,
|
|
121
|
-
otp: false,
|
|
122
|
-
encode: async (value, encryptedKey, sub) =>
|
|
123
|
-
createSecretHash(value).then((hash) => encrypt(hash, encryptedKey, sub)),
|
|
124
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub),
|
|
125
|
-
verify: async (value, hash) => verifySecretHash(hash, value)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// aka Password/Credential Recovery
|
|
129
|
-
export const passwordToken = {
|
|
130
|
-
type: 'token',
|
|
131
|
-
entropy: 20,
|
|
132
|
-
charPool: characterPoolSize.numeric,
|
|
133
|
-
otp: true,
|
|
134
|
-
expire: 30,
|
|
135
|
-
create: async () => randomNumeric(passwordToken.entropy),
|
|
136
|
-
encode: async (value, encryptedKey, sub) =>
|
|
137
|
-
createSecretHash(value).then((hash) => encrypt(hash, encryptedKey, sub)),
|
|
138
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub),
|
|
139
|
-
verify: async (value, hash) => verifySecretHash(hash, value)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export const oneTimeSecret = {
|
|
143
|
-
type: 'secret',
|
|
144
|
-
entropy: 64, //
|
|
145
|
-
charPool: characterPoolSize.base64,
|
|
146
|
-
otp: false,
|
|
147
|
-
expire: null,
|
|
148
|
-
create: async () => randomBase64(oneTimeSecret.entropy),
|
|
149
|
-
encode: async (value, encryptedKey, sub) => encrypt(value, encryptedKey, sub),
|
|
150
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub)
|
|
151
|
-
// create, hash, verify to be handled within
|
|
152
|
-
}
|
|
153
|
-
export const oneTimeToken = {
|
|
154
|
-
type: 'token',
|
|
155
|
-
entropy: 20,
|
|
156
|
-
charPool: characterPoolSize.numeric,
|
|
157
|
-
otp: true,
|
|
158
|
-
expire: 30
|
|
159
|
-
// create, hash, verify to be handled within
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// aka lookup secret
|
|
163
|
-
export const recoveryCode = {
|
|
164
|
-
type: 'secret',
|
|
165
|
-
entropy: 112,
|
|
166
|
-
charPool: characterPoolSize.alphaNumeric,
|
|
167
|
-
otp: true,
|
|
168
|
-
create: async () => randomAlphaNumeric(recoveryCode.entropy),
|
|
169
|
-
encode: async (value, encryptedKey, sub) =>
|
|
170
|
-
createSecretHash(value).then((hash) => encrypt(hash, encryptedKey, sub)),
|
|
171
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub),
|
|
172
|
-
verify: async (value, hash) => verifySecretHash(hash, value)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export const outOfBandToken = {
|
|
176
|
-
type: 'token',
|
|
177
|
-
entropy: 20,
|
|
178
|
-
charPool: characterPoolSize.numeric,
|
|
179
|
-
otp: true,
|
|
180
|
-
expire: 10 * 60,
|
|
181
|
-
create: async () => randomNumeric(outOfBandToken.entropy),
|
|
182
|
-
encode: async (value, encryptedKey, sub) =>
|
|
183
|
-
createSecretHash(value).then((hash) => encrypt(hash, encryptedKey, sub)),
|
|
184
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub),
|
|
185
|
-
verify: async (value, hash) => verifySecretHash(hash, value)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export const accessToken = {
|
|
189
|
-
type: 'secret',
|
|
190
|
-
entropy: 112,
|
|
191
|
-
charPool: characterPoolSize.alphaNumeric,
|
|
192
|
-
otp: false,
|
|
193
|
-
expire: 30 * 24 * 60 * 60, // allow override from user
|
|
194
|
-
create: async () => randomAlphaNumeric(accessToken.entropy),
|
|
195
|
-
encode: async (value, encryptedKey, sub) =>
|
|
196
|
-
createSecretHash(value).then((hash) => encrypt(hash, encryptedKey, sub)),
|
|
197
|
-
decode: async (value, encryptedKey, sub) => decrypt(value, encryptedKey, sub),
|
|
198
|
-
verify: async (value, hash) => verifySecretHash(hash, value)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
138
|
// *** Helpers *** //
|
|
202
|
-
|
|
139
|
+
// Ref: https://therootcompany.com/blog/how-many-bits-of-entropy-per-character/
|
|
140
|
+
export const entropyToCharacterLength = (bits, characterPoolSize) => {
|
|
141
|
+
// bits*ln(2)/ln(characterPoolSize)
|
|
142
|
+
return Math.ceil((bits * Math.LN2) / Math.log(characterPoolSize));
|
|
143
|
+
};
|
|
144
|
+
/* export const characterLengthToEntropy = (
|
|
203
145
|
characterLength,
|
|
204
|
-
characterPoolSize
|
|
146
|
+
characterPoolSize,
|
|
205
147
|
) => {
|
|
206
148
|
// log_2(characterPoolSize^characterLength)
|
|
207
|
-
return Math.
|
|
208
|
-
}
|
|
149
|
+
return Math.floor(Math.log2(characterPoolSize ** characterLength));
|
|
150
|
+
}; */
|
|
209
151
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
152
|
+
// *** Random generators *** //
|
|
153
|
+
export { randomBytes, randomInt, randomUUID } from "node:crypto";
|
|
154
|
+
|
|
155
|
+
export const charactersNumeric = "0123456789";
|
|
156
|
+
export const charactersAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
157
|
+
export const charactersAlphaLower = "abcdefghijklmnopqrstuvwxyz";
|
|
158
|
+
export const charactersAlpha = charactersAlphaUpper + charactersAlphaLower;
|
|
159
|
+
export const charactersAlphaNumeric = charactersAlpha + charactersNumeric;
|
|
160
|
+
export const charactersDistinguishable = "CDEHKMPRTUWXY012458";
|
|
161
|
+
|
|
162
|
+
const randomCharactersCache = {
|
|
163
|
+
charactersAlphaNumeric: customAlphabet(charactersAlphaNumeric),
|
|
164
|
+
};
|
|
165
|
+
export const randomCharacters = (
|
|
166
|
+
length,
|
|
167
|
+
characters = charactersAlphaNumeric,
|
|
168
|
+
) => {
|
|
169
|
+
randomCharactersCache[characters] ??= customAlphabet(characters);
|
|
170
|
+
return randomCharactersCache[characters](length);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const randomAlphaNumeric = (characterLength) => {
|
|
174
|
+
return randomCharacters(characterLength, charactersAlphaNumeric);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export const randomNumeric = (characterLength) => {
|
|
178
|
+
let value = "";
|
|
179
|
+
for (let i = characterLength; i--; ) {
|
|
180
|
+
value += randomInt(9);
|
|
181
|
+
}
|
|
182
|
+
return value;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// *** configs *** //
|
|
186
|
+
// Input: {id, prefix, entropy, characters, opt, expire}
|
|
187
|
+
// Output: {id, type, opt, expire, create, ...}
|
|
188
|
+
export const makeRandomConfigObject = ({
|
|
189
|
+
id,
|
|
190
|
+
prefix = "",
|
|
191
|
+
entropy = 64,
|
|
192
|
+
characters = charactersAlphaNumeric,
|
|
193
|
+
...params
|
|
194
|
+
} = {}) => {
|
|
195
|
+
const minLength = entropyToCharacterLength(entropy, characters.length);
|
|
196
|
+
const config = {
|
|
197
|
+
id,
|
|
198
|
+
type: "id",
|
|
199
|
+
create: () => prefix + randomCharacters(minLength, characters),
|
|
200
|
+
...params,
|
|
201
|
+
};
|
|
202
|
+
return config;
|
|
203
|
+
};
|
|
214
204
|
|
|
215
205
|
// *** Digests *** //
|
|
216
|
-
export const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
206
|
+
export const randomChecksumSalt = () => {
|
|
207
|
+
return randomBytes(32); // 256 bits
|
|
208
|
+
};
|
|
209
|
+
export const randomChecksumPepper = () => {
|
|
210
|
+
return randomIV(); // 96
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export const createSaltedValue = (value, { checksumSalt } = {}) => {
|
|
214
|
+
checksumSalt ??= options.digestChecksumSalt;
|
|
215
|
+
if (!checksumSalt) {
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
const newValue = value + checksumSalt;
|
|
219
|
+
return newValue;
|
|
220
|
+
};
|
|
221
|
+
export const createPepperedValue = (
|
|
222
|
+
value,
|
|
223
|
+
{ checksumPepper, encryptionKey } = {},
|
|
224
|
+
) => {
|
|
225
|
+
checksumPepper ??= options.digestChecksumPepper;
|
|
226
|
+
encryptionKey ??= options.symmetricEncryptionKey;
|
|
227
|
+
if (!checksumPepper || !encryptionKey) {
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
230
|
+
const newValue = symmetricEncrypt(value, {
|
|
231
|
+
encryptionKey,
|
|
232
|
+
sub: "",
|
|
233
|
+
iv: checksumPepper,
|
|
234
|
+
});
|
|
235
|
+
return newValue;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export const createChecksum = (value, { hashAlgorithm, encoding } = {}) => {
|
|
239
|
+
hashAlgorithm ??= options.digestChecksumHashAlgorithm;
|
|
240
|
+
encoding ??= options.digestChecksumEncoding;
|
|
241
|
+
return createHash(hashAlgorithm).update(value).digest(encoding);
|
|
242
|
+
};
|
|
243
|
+
export const createSeasonedChecksum = (
|
|
244
|
+
value,
|
|
245
|
+
{ hashAlgorithm, encoding, checksumSalt, checksumPepper } = {},
|
|
246
|
+
) => {
|
|
247
|
+
return createChecksum(
|
|
248
|
+
createPepperedValue(createSaltedValue(value, { checksumSalt }), {
|
|
249
|
+
checksumPepper,
|
|
250
|
+
}),
|
|
251
|
+
{
|
|
252
|
+
hashAlgorithm,
|
|
253
|
+
encoding,
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export const createDigest = (value, { hashAlgorithm, encoding } = {}) => {
|
|
259
|
+
hashAlgorithm ??= options.digestChecksumHashAlgorithm;
|
|
260
|
+
const checksum = createChecksum(value, { hashAlgorithm, encoding });
|
|
261
|
+
return `${hashAlgorithm}:${checksum}`;
|
|
262
|
+
};
|
|
263
|
+
export const createSaltedDigest = (
|
|
264
|
+
value,
|
|
265
|
+
{ hashAlgorithm, encoding, checksumSalt } = {},
|
|
266
|
+
) => {
|
|
267
|
+
hashAlgorithm ??= options.digestChecksumHashAlgorithm;
|
|
268
|
+
const checksum = createChecksum(createSaltedValue(value, { checksumSalt }), {
|
|
269
|
+
hashAlgorithm,
|
|
270
|
+
encoding,
|
|
271
|
+
});
|
|
272
|
+
return `${hashAlgorithm}:${checksum}`;
|
|
273
|
+
};
|
|
274
|
+
export const createPepperedDigest = (
|
|
275
|
+
value,
|
|
276
|
+
{ hashAlgorithm, encoding, checksumPepper, encryptionKey } = {},
|
|
277
|
+
) => {
|
|
278
|
+
hashAlgorithm ??= options.digestChecksumHashAlgorithm;
|
|
279
|
+
const checksum = createChecksum(
|
|
280
|
+
createPepperedValue(value, { checksumPepper, encryptionKey }),
|
|
281
|
+
{
|
|
282
|
+
hashAlgorithm,
|
|
283
|
+
encoding,
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
return `${hashAlgorithm}:${checksum}`;
|
|
287
|
+
};
|
|
288
|
+
export const createSeasonedDigest = (
|
|
289
|
+
value,
|
|
290
|
+
{ hashAlgorithm, encoding, checksumSalt, checksumPepper, encryptionKey } = {},
|
|
291
|
+
) => {
|
|
292
|
+
hashAlgorithm ??= options.digestChecksumHashAlgorithm;
|
|
293
|
+
const checksum = createSeasonedChecksum(value, {
|
|
294
|
+
hashAlgorithm,
|
|
295
|
+
encoding,
|
|
296
|
+
checksumSalt,
|
|
297
|
+
checksumPepper,
|
|
298
|
+
encryptionKey,
|
|
299
|
+
});
|
|
300
|
+
return `${hashAlgorithm}:${checksum}`;
|
|
301
|
+
};
|
|
224
302
|
|
|
225
303
|
// *** Hashing *** //
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
304
|
+
const argon2Options = {
|
|
305
|
+
algorithm: "argon2id",
|
|
306
|
+
version: 19,
|
|
307
|
+
parallelism: 1, // Default 1
|
|
308
|
+
memoryCost: 15, // memory 2^memoryCost // Default 2 ** 12 = 4MB
|
|
309
|
+
timeCost: 3, // Default 3
|
|
310
|
+
nonceLength: 16,
|
|
311
|
+
hashLength: 64, // hashLength: 128 // Default 32
|
|
312
|
+
|
|
313
|
+
secret: undefined, // pepper
|
|
314
|
+
associatedData: undefined, // sub
|
|
315
|
+
};
|
|
316
|
+
export const encodeArgon2 = ({
|
|
317
|
+
algorithm,
|
|
318
|
+
version,
|
|
319
|
+
memoryCost,
|
|
320
|
+
timeCost,
|
|
321
|
+
parallelism,
|
|
322
|
+
nonce,
|
|
323
|
+
hash,
|
|
324
|
+
} = {}) => {
|
|
325
|
+
return `$${algorithm}$v=${version}$m=${memoryCost},t=${timeCost},p=${parallelism}$${nonce.toString(options.defaultEncoding)}$${hash.toString(options.defaultEncoding)}`;
|
|
326
|
+
};
|
|
327
|
+
export const decodeArgon2 = (str) => {
|
|
328
|
+
const argon2Options = {};
|
|
329
|
+
const optionMap = {
|
|
330
|
+
m: "memoryCost",
|
|
331
|
+
t: "timeCost",
|
|
332
|
+
p: "parallelism",
|
|
333
|
+
};
|
|
334
|
+
let [, algorithm, version, variables, nonce, hash] = str.split("$");
|
|
335
|
+
|
|
336
|
+
if (version) {
|
|
337
|
+
version = Number.parseInt(version.replace("v=", ""), 10);
|
|
338
|
+
}
|
|
339
|
+
nonce = Buffer.from(nonce, options.defaultEncoding);
|
|
340
|
+
hash = Buffer.from(hash, options.defaultEncoding);
|
|
341
|
+
const nonceLength = Buffer.byteLength(nonce);
|
|
342
|
+
const hashLength = Buffer.byteLength(hash);
|
|
343
|
+
Object.assign(argon2Options, {
|
|
344
|
+
algorithm,
|
|
345
|
+
version,
|
|
346
|
+
nonce,
|
|
347
|
+
nonceLength,
|
|
348
|
+
hash,
|
|
349
|
+
hashLength,
|
|
350
|
+
});
|
|
351
|
+
for (const pair of variables.split(",")) {
|
|
352
|
+
const [key, value] = pair.split("=");
|
|
353
|
+
argon2Options[optionMap[key]] = Number.parseInt(value, 10);
|
|
354
|
+
}
|
|
355
|
+
return argon2Options;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
export const createArgon2 = (
|
|
359
|
+
message,
|
|
360
|
+
{
|
|
361
|
+
algorithm,
|
|
362
|
+
version,
|
|
363
|
+
parallelism,
|
|
364
|
+
memoryCost,
|
|
365
|
+
timeCost,
|
|
366
|
+
nonceLength,
|
|
367
|
+
hashLength,
|
|
368
|
+
} = {},
|
|
369
|
+
) => {
|
|
370
|
+
algorithm ??= options.secretArgon2Algorithm;
|
|
371
|
+
version ??= options.secretArgon2Version;
|
|
372
|
+
memoryCost ??= options.secretArgon2MemoryCost;
|
|
373
|
+
timeCost ??= options.secretArgon2TimeCost;
|
|
374
|
+
parallelism ??= options.secretArgon2Parallelism;
|
|
375
|
+
nonceLength ??= options.secretArgon2NonceLength;
|
|
376
|
+
hashLength ??= options.secretArgon2HashLength;
|
|
377
|
+
|
|
378
|
+
const nonce = randomBytes(nonceLength);
|
|
379
|
+
const hash = argon2Sync(algorithm, {
|
|
380
|
+
message,
|
|
381
|
+
nonce,
|
|
382
|
+
parallelism,
|
|
383
|
+
memory: 2 ** memoryCost,
|
|
384
|
+
passes: timeCost,
|
|
385
|
+
tagLength: hashLength,
|
|
386
|
+
});
|
|
387
|
+
return encodeArgon2({
|
|
388
|
+
algorithm,
|
|
389
|
+
version,
|
|
390
|
+
memoryCost,
|
|
391
|
+
timeCost,
|
|
392
|
+
parallelism,
|
|
393
|
+
nonce,
|
|
394
|
+
hash,
|
|
395
|
+
});
|
|
396
|
+
};
|
|
397
|
+
export const verifyArgon2 = (derivedKey, message) => {
|
|
398
|
+
const {
|
|
399
|
+
algorithm,
|
|
400
|
+
memoryCost,
|
|
401
|
+
timeCost,
|
|
402
|
+
parallelism,
|
|
403
|
+
nonce,
|
|
404
|
+
hash,
|
|
405
|
+
hashLength,
|
|
406
|
+
} = decodeArgon2(derivedKey);
|
|
407
|
+
|
|
408
|
+
const verifyHash = argon2Sync(algorithm, {
|
|
409
|
+
message,
|
|
410
|
+
nonce,
|
|
411
|
+
parallelism,
|
|
412
|
+
memory: 2 ** memoryCost,
|
|
413
|
+
passes: timeCost,
|
|
414
|
+
tagLength: hashLength,
|
|
415
|
+
});
|
|
416
|
+
return timingSafeEqual(hash, verifyHash);
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
export const createSecretHash = async (value, options) => {
|
|
420
|
+
return createArgon2(value, options);
|
|
421
|
+
};
|
|
239
422
|
|
|
240
423
|
export const verifySecretHash = async (hash, value) => {
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// *** Encryption *** //
|
|
245
|
-
const authTagLength = 16
|
|
246
|
-
|
|
247
|
-
export const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
424
|
+
return verifyArgon2(hash, value);
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// *** Symmetric Encryption *** //
|
|
428
|
+
const authTagLength = 16;
|
|
429
|
+
|
|
430
|
+
export const symmetricRandomEncryptionKey = () => {
|
|
431
|
+
return randomBytes(32); // 256 bits
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
export const randomIV = () => {
|
|
435
|
+
return randomBytes(12); // 96 bits
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
export const symmetricGenerateEncryptionKey = (
|
|
439
|
+
sub,
|
|
440
|
+
{ encryptionKey, signatureSecret } = {},
|
|
441
|
+
) => {
|
|
442
|
+
encryptionKey ??= options.symmetricEncryptionKey;
|
|
443
|
+
signatureSecret ??= options.symemeticSignatureSecret;
|
|
444
|
+
|
|
445
|
+
const rowEncryptionKey = symmetricRandomEncryptionKey();
|
|
446
|
+
const rowEncryptedKey = symmetricEncrypt(rowEncryptionKey, {
|
|
447
|
+
encryptionKey,
|
|
448
|
+
signatureSecret,
|
|
449
|
+
sub,
|
|
450
|
+
});
|
|
451
|
+
return { encryptionKey: rowEncryptionKey, encryptedKey: rowEncryptedKey };
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// sub add context to encryption
|
|
455
|
+
export const symmetricEncryptFields = (
|
|
456
|
+
values,
|
|
457
|
+
{ encryptedKey, encryptionKey, signatureSecret, sub },
|
|
458
|
+
fields = [],
|
|
459
|
+
) => {
|
|
460
|
+
if (encryptedKey) {
|
|
461
|
+
encryptionKey ??= symmetricDecryptKey(encryptedKey, {
|
|
462
|
+
signatureSecret,
|
|
463
|
+
sub,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
if (!encryptionKey) return values;
|
|
467
|
+
const encryptedValues = structuredClone(values);
|
|
468
|
+
for (const key of fields) {
|
|
469
|
+
encryptedValues[key] &&= symmetricEncrypt(encryptedValues[key], {
|
|
470
|
+
encryptionKey,
|
|
471
|
+
signatureSecret,
|
|
472
|
+
sub,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
return encryptedValues;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
export const symmetricEncrypt = (
|
|
479
|
+
data,
|
|
480
|
+
{ encryptedKey, encryptionKey, signatureSecret, sub, decoding, encoding, iv },
|
|
481
|
+
) => {
|
|
482
|
+
if (encryptedKey) {
|
|
483
|
+
encryptionKey ??= symmetricDecryptKey(encryptedKey, {
|
|
484
|
+
signatureSecret,
|
|
485
|
+
sub,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
if (!encryptionKey || !data) return data;
|
|
489
|
+
decoding ??= "utf8";
|
|
490
|
+
encoding ??= options.symmetricEncryptionEncoding;
|
|
491
|
+
iv ??= randomIV();
|
|
492
|
+
const cipher = createCipheriv(
|
|
493
|
+
options.symmetricEncryptionAlgorithm,
|
|
494
|
+
encryptionKey,
|
|
495
|
+
iv,
|
|
496
|
+
{
|
|
497
|
+
authTagLength,
|
|
498
|
+
},
|
|
499
|
+
);
|
|
500
|
+
cipher.setAAD(sub);
|
|
501
|
+
const encryptedData =
|
|
502
|
+
cipher.update(data, decoding, encoding) + cipher.final(encoding);
|
|
503
|
+
const authTag = cipher.getAuthTag();
|
|
504
|
+
|
|
505
|
+
const encryptedDataPacket =
|
|
506
|
+
iv.toString(encoding) + authTag.toString(encoding) + encryptedData;
|
|
507
|
+
|
|
508
|
+
// add signature to end
|
|
509
|
+
return symmetricSignatureSign(encryptedDataPacket, { signatureSecret });
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
export const symmetricDecryptFields = (
|
|
513
|
+
encryptedValues,
|
|
514
|
+
{ encryptedKey, encryptionKey, signatureSecret, sub },
|
|
515
|
+
fields = [],
|
|
516
|
+
) => {
|
|
517
|
+
if (encryptedKey) {
|
|
518
|
+
encryptionKey ??= symmetricDecryptKey(encryptedKey, {
|
|
519
|
+
signatureSecret,
|
|
520
|
+
sub,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
if (!encryptionKey) return encryptedValues;
|
|
524
|
+
const values = structuredClone(encryptedValues);
|
|
525
|
+
for (const key of fields) {
|
|
526
|
+
values[key] &&= symmetricDecrypt(values[key], {
|
|
527
|
+
encryptionKey,
|
|
528
|
+
signatureSecret,
|
|
529
|
+
sub,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
return values;
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
export const symmetricDecryptKey = (
|
|
536
|
+
encryptedKey,
|
|
537
|
+
{ sub, encryptionKey, signatureSecret } = {},
|
|
538
|
+
) => {
|
|
539
|
+
encryptionKey ??= options.symmetricEncryptionKey;
|
|
540
|
+
signatureSecret ??= options.symemeticSignatureSecret;
|
|
541
|
+
return Buffer.from(
|
|
542
|
+
symmetricDecrypt(encryptedKey, {
|
|
543
|
+
encryptionKey,
|
|
544
|
+
signatureSecret,
|
|
545
|
+
sub,
|
|
546
|
+
encoding: options.symmetricEncryptionEncoding,
|
|
547
|
+
}),
|
|
548
|
+
options.symmetricEncryptionEncoding,
|
|
549
|
+
);
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
export const symmetricDecrypt = (
|
|
553
|
+
signedEncryptedDataPacket,
|
|
554
|
+
{ encryptedKey, encryptionKey, signatureSecret, sub, decoding, encoding },
|
|
555
|
+
) => {
|
|
556
|
+
if (encryptedKey) {
|
|
557
|
+
encryptionKey ??= symmetricDecryptKey(encryptedKey, {
|
|
558
|
+
signatureSecret,
|
|
559
|
+
sub,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
if (!encryptionKey || !signedEncryptedDataPacket)
|
|
563
|
+
return signedEncryptedDataPacket;
|
|
564
|
+
decoding ??= options.symmetricEncryptionEncoding;
|
|
565
|
+
encoding ??= "utf8";
|
|
566
|
+
|
|
567
|
+
// remove signature when successful
|
|
568
|
+
const encryptedDataPacket = symmetricSignatureVerify(
|
|
569
|
+
signedEncryptedDataPacket,
|
|
570
|
+
{
|
|
571
|
+
signatureSecret,
|
|
572
|
+
},
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
if (encryptedDataPacket === false) {
|
|
576
|
+
throw new Error("Signature incorrect");
|
|
577
|
+
}
|
|
578
|
+
const iv = Buffer.from(
|
|
579
|
+
encryptedDataPacket.substring(0, symmetricEncryptionEncodingLengths.iv),
|
|
580
|
+
decoding,
|
|
581
|
+
);
|
|
582
|
+
const authTag = Buffer.from(
|
|
583
|
+
encryptedDataPacket.substring(
|
|
584
|
+
symmetricEncryptionEncodingLengths.iv,
|
|
585
|
+
symmetricEncryptionEncodingLengths.ivAndAuthTag,
|
|
586
|
+
),
|
|
587
|
+
decoding,
|
|
588
|
+
);
|
|
589
|
+
const encryptedData = Buffer.from(
|
|
590
|
+
encryptedDataPacket.substring(
|
|
591
|
+
symmetricEncryptionEncodingLengths.ivAndAuthTag,
|
|
592
|
+
),
|
|
593
|
+
decoding,
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const decipher = createDecipheriv(
|
|
597
|
+
options.symmetricEncryptionAlgorithm,
|
|
598
|
+
encryptionKey,
|
|
599
|
+
iv,
|
|
600
|
+
{
|
|
601
|
+
authTagLength,
|
|
602
|
+
},
|
|
603
|
+
);
|
|
604
|
+
decipher.setAAD(sub);
|
|
605
|
+
|
|
606
|
+
decipher.setAuthTag(authTag);
|
|
607
|
+
const data =
|
|
608
|
+
decipher.update(encryptedData, decoding, encoding) +
|
|
609
|
+
decipher.final(encoding);
|
|
610
|
+
return data;
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
// *** Symmetric Signatures *** //
|
|
614
|
+
export const symmetricRandomSignatureSecret = () => {
|
|
615
|
+
return randomBytes(32); // 256 bits
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
export const symmetricGenerateSignatureSecret = () => {
|
|
619
|
+
const signatureSecret = symmetricRandomSignatureSecret();
|
|
620
|
+
return { signatureSecret };
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
export const symmetricSignatureSign = (
|
|
624
|
+
data,
|
|
625
|
+
{ hashAlgorithm, signatureSecret } = {},
|
|
626
|
+
) => {
|
|
627
|
+
signatureSecret ??= options.symmetricSignatureSecret;
|
|
628
|
+
hashAlgorithm ??= options.symmetricSignatureHashAlgorithm;
|
|
629
|
+
const signature = createHmac(hashAlgorithm, signatureSecret)
|
|
630
|
+
.update(data)
|
|
631
|
+
.digest(options.symmetricSignatureEncoding)
|
|
632
|
+
.replace(/=+$/, "");
|
|
633
|
+
|
|
634
|
+
const signedData = `${data}.${signature}`;
|
|
635
|
+
return signedData;
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
export const symmetricSignatureVerify = (
|
|
639
|
+
signedData,
|
|
640
|
+
{ hashAlgorithm, signatureSecret } = {},
|
|
641
|
+
) => {
|
|
642
|
+
if (typeof signedData !== "string") return false;
|
|
643
|
+
let lastIndexOf = signedData.lastIndexOf(".");
|
|
644
|
+
// Test for unsigned
|
|
645
|
+
if (lastIndexOf < 0) {
|
|
646
|
+
lastIndexOf = signedData.length;
|
|
647
|
+
}
|
|
648
|
+
const data = signedData.substring(0, lastIndexOf);
|
|
649
|
+
const signedDataExpected = symmetricSignatureSign(data, {
|
|
650
|
+
hashAlgorithm,
|
|
651
|
+
signatureSecret,
|
|
652
|
+
});
|
|
653
|
+
return safeEqual(signedData, signedDataExpected) && data;
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
// Allow rotation of global encryption key, global signature secret, and row encryption key
|
|
657
|
+
export const symmetricRotation = (
|
|
658
|
+
oldEncryptedValues,
|
|
659
|
+
oldOptions, // { encryptionKey, signatureSecret, sub, decoding, encoding }, // old
|
|
660
|
+
oldFields,
|
|
661
|
+
newOptions, // { encryptionKey, signatureSecret, sub, decoding, encoding }, // new
|
|
662
|
+
newFields,
|
|
663
|
+
transform = (data) => {
|
|
664
|
+
return data;
|
|
665
|
+
},
|
|
287
666
|
) => {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
667
|
+
if (oldOptions.sub !== newOptions.sub) throw new Error("Mismatching `sub`");
|
|
668
|
+
|
|
669
|
+
const oldEncryptedValuesClone = structuredClone(oldEncryptedValues);
|
|
670
|
+
// Don't use structuredClone, converts Buffer to Uint8Array ...
|
|
671
|
+
const oldOptionsClone = { ...oldOptions };
|
|
672
|
+
const newOptionsClone = { ...(newOptions ?? oldOptions) };
|
|
673
|
+
|
|
674
|
+
// decrypt old encryption key
|
|
675
|
+
const { encryptionKey: oldEncryptedKey } = oldEncryptedValuesClone;
|
|
676
|
+
oldEncryptedValuesClone.encryptionKey = undefined;
|
|
677
|
+
|
|
678
|
+
const oldEncryptionKey = symmetricDecryptKey(
|
|
679
|
+
oldEncryptedKey,
|
|
680
|
+
oldOptionsClone,
|
|
681
|
+
);
|
|
682
|
+
oldOptionsClone.encryptionKey = oldEncryptionKey;
|
|
683
|
+
|
|
684
|
+
// decrypt
|
|
685
|
+
const data = transform(
|
|
686
|
+
symmetricDecryptFields(
|
|
687
|
+
oldEncryptedValuesClone,
|
|
688
|
+
{ ...oldOptionsClone, encryptionKey: oldEncryptionKey },
|
|
689
|
+
oldFields,
|
|
690
|
+
),
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
// rotate encryptionKey
|
|
694
|
+
const { encryptionKey: newEncryptionKey, encryptedKey: newEncryptedKey } =
|
|
695
|
+
symmetricGenerateEncryptionKey(newOptionsClone.sub, newOptionsClone);
|
|
696
|
+
newOptionsClone.encryptionKey = newEncryptionKey;
|
|
697
|
+
|
|
698
|
+
// encrypt
|
|
699
|
+
const newEncryptedValues = symmetricEncryptFields(
|
|
700
|
+
data,
|
|
701
|
+
newOptionsClone,
|
|
702
|
+
newFields ?? oldFields,
|
|
703
|
+
);
|
|
704
|
+
newEncryptedValues.encryptionKey = newEncryptedKey;
|
|
705
|
+
|
|
706
|
+
return newEncryptedValues;
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// *** Asymmetric Signatures *** //
|
|
710
|
+
// asymmetricKeyPairType
|
|
711
|
+
export const makeAsymmetricKeys = async () => {
|
|
712
|
+
const { publicKey, privateKey } = await generateKeyPair("ec", {
|
|
713
|
+
namedCurve: options.asymmetricKeyNamedCurve,
|
|
714
|
+
paramEncoding: "named",
|
|
715
|
+
publicKeyEncoding: {
|
|
716
|
+
type: "spki",
|
|
717
|
+
format: "pem",
|
|
718
|
+
},
|
|
719
|
+
privateKeyEncoding: {
|
|
720
|
+
type: "sec1",
|
|
721
|
+
format: "pem",
|
|
722
|
+
// Encryption done at another level for consistency
|
|
723
|
+
// cipher: options.asymmetricEncryptionAlgorithm,
|
|
724
|
+
// passphrase: encryptionKey,
|
|
725
|
+
},
|
|
726
|
+
});
|
|
727
|
+
return { publicKey, privateKey };
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
export const makeAsymmetricSignature = async (
|
|
731
|
+
data,
|
|
732
|
+
privateKey,
|
|
733
|
+
{ hashAlgorithm } = {},
|
|
328
734
|
) => {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
)
|
|
341
|
-
decipher.setAAD(Buffer.from(assocData, 'utf8'))
|
|
342
|
-
decipher.setAuthTag(authTag)
|
|
343
|
-
return (
|
|
344
|
-
decipher.update(encryptedData, decoding, encoding) +
|
|
345
|
-
decipher.final(encoding)
|
|
346
|
-
)
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// *** Signatures *** //
|
|
350
|
-
export const makeAsymmetricKeys = async (encryptionKey) => {
|
|
351
|
-
const { publicKey, privateKey } = await generateKeyPair('ec', {
|
|
352
|
-
namedCurve: 'P-384', // P-512
|
|
353
|
-
paramEncoding: 'named',
|
|
354
|
-
publicKeyEncoding: {
|
|
355
|
-
type: 'spki',
|
|
356
|
-
format: 'pem'
|
|
357
|
-
},
|
|
358
|
-
privateKeyEncoding: {
|
|
359
|
-
type: 'sec1',
|
|
360
|
-
format: 'pem',
|
|
361
|
-
// TODO remove encryption for other with sub check?
|
|
362
|
-
cipher: options.encryptionMethod,
|
|
363
|
-
passphrase: encryptionKey
|
|
364
|
-
}
|
|
365
|
-
})
|
|
366
|
-
return { publicKey, privateKey }
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
export const makeSignature = (data, privateKey, algorithm = 'SHA3-256') => {
|
|
370
|
-
return sign(algorithm, Buffer.from(data), privateKey).toString('base64')
|
|
371
|
-
}
|
|
372
|
-
export const verifySignature = (
|
|
373
|
-
data,
|
|
374
|
-
publicKey,
|
|
375
|
-
signature,
|
|
376
|
-
algorithm = 'SHA3-256'
|
|
735
|
+
hashAlgorithm ??= options.asymmetricSignatureHashAlgorithm;
|
|
736
|
+
return (await sign(hashAlgorithm, Buffer.from(data), privateKey)).toString(
|
|
737
|
+
options.asymmetricSignatureEncoding,
|
|
738
|
+
);
|
|
739
|
+
};
|
|
740
|
+
export const verifyAsymmetricSignature = async (
|
|
741
|
+
data,
|
|
742
|
+
publicKey,
|
|
743
|
+
signature,
|
|
744
|
+
{ hashAlgorithm } = {},
|
|
377
745
|
) => {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
// 'b9ebfdab6603dd8e151138ec8298aaf8b49bf6d38480f9864e9e72e65c4d8b59.b9f9bc89d7f3ce4aa1e1af469d10508b.aa1b6a682fcc186e867a9cfd'
|
|
396
|
-
// const value =
|
|
397
|
-
// '76ebdea75ea529c16f1645d5435413934cbb97378293776cb446331e6d8323865a808b7f0822cd4f36ef6cca57354ea7c955c94d232a55038321184f4294971315fda4b8dc2f33422bbd3a5c978edc0f5bf019f818f0c92f7c570bdc49fe2f71df3856655198b6723fa52697b9225b2e79f04ed80cd35ef61a8e1e06c3751d3f9caa40aa9a41528ce5ab1bfae8ecff0feab784497ccbf2c7dca4461c6b9a74d1ded6233dc38ad75d0fe2f57cfda2bd1d59ed1f8c53de911cf8071dc7cefde7870c303137586bc0c5f85fe93505d9056994a4d8e59df2027b564c303aa723e98ae5934b15039f90d21827ac22b824593f334466622582a2857f04d8bf450310a9924060d113d2ece0259da68376b31a3c2e8a0b79e81f8efe96bf2b7a6b5b1841edca2566a0a89ab80ee6d6ac2ed24e788ad318a1238dfa5b9e965d256935d3aeafe52f986a530b2f9225bbe7a73b43e6515ce789701f501546239460bccc46285f3fcff0c54c2b6df820334a974abea81460fd4ac99a13cb88e296a2275f80f4c783757319417078458977461dd5e101f9f9b7d303578379dc15ee1b549e3fcceffffba895ecc8291d1f82e150ce8e10640fd79f2e0666167ef7d529cd28cb76a72bf2b3e239dbb845bedd39f42c938bbe711542231fb835edb547d919b3c695272f4cd43640eb3bed2b86948278e5aeeb67ef932ef08332730e75752fd5bd247e2dd30470fc493362d2ecac72237dfbf0a1694f8e6b8c18ab24c71eb723a3f610a056a73fea90f4f8660bfc6dec46fa52a55a3d72a5c9b97770bff7d52ea91b97cc643d915341642e201918fcb088986e9134e133519c6575048e3f840622e569fc5fa66e4f0c31681c9de4b1b1f6acb179728bc0a9f9950bf2e0c8d49da835298013977a2f97d7052bcbedbc39b875bbf9a0622ede589810175ab2455ea72a8274659af7066a2c97ba3aacce84531d8685d08b15d3619038997ae459ac916a88bedaf619f574236d1b86b02f29edaf78684c91c6ee45199b861359ee374fecb95be8a85dc012991d22564adeb2ec95f160574858fa010c40ce74077a08f0e681eb4b10a0093e885f115a12469a82b07012ad4e20b836d5711358c47969af89d09c9299edc540321fe897312ca49c9e8f93d50dc68394d0cc1d108f38e3534821b979c405c1f526a3b57651897873aeeb0909b176fb2034f9fdb1d43a8003809af29ff0bdc456a593029c4526b270908ac94b620a7cb8b7da20a6421e0f2671c899c20d78e1e0a1c3ab0b92948c90fca3c36d21a5e53143bc7e061773251364f8ce3b8992b6313c9604dc19c018b9efc6ca8f7c1fe039077fd76775461d1fa3968f51be7e340e3f09b40d6b44c77a7cbf7d71cad2ddea1121b31d27470a38cda8f8d38170a0d8f0b02d56f8e97b2134597d6111ea6670545bd4f7b4e4d6d7cf2fb5b787a68ba8ee84775f06db195f47282d9646dfbdbb57701e2ef5a432b621cb54a70e94de6b7a3f95bfe3c50793e08a27f76f832070fb2d23ed19dcd75f798aeff0c3e76d8215d43e1a8b7e643bffa243691adf2abcd52f465390e92e0344e2f452673f12d50e2ead0496ba76504ae0f78c81f2a2b5a64b39d3fb35f39f178b9a1587d1f07ed28e7696bfc8740f429f6cc35c964ae04ef39c1070eab6415be3aab887ff6192ac4d49ba9b169221b47d73c55f6010dda07a77cbcf54b779d380591786ba70c6d7000af57034539487f801aeb5c097939682e1530bc26c0b030bcab327ddaaaac5c9b969301b6d4ed7f601a46e8431e8cae3800c80c42686fee3b7625742798ced5976d163d2c8aad1bdac36c001e4e246fdb7b4c6850ed99554cfc0c07b7c7acc7659fd042989cc291ec2bd03a59e439860a5c48e430780f9d85c9acb3e85e741e73163f29afb7c43f143e7ada99a4670cbbe2210bfce8d36ee86.a5237f2d798c9f98b26a676794d3c3f6.0f15d0f53657150aeb1462d5'
|
|
398
|
-
// const output = decrypt(value, encryptionKey, assocData)
|
|
399
|
-
// console.log(output)
|
|
400
|
-
// *** TOTP *** //
|
|
746
|
+
hashAlgorithm ??= options.asymmetricSignatureHashAlgorithm;
|
|
747
|
+
return await verify(
|
|
748
|
+
hashAlgorithm,
|
|
749
|
+
Buffer.from(data),
|
|
750
|
+
publicKey,
|
|
751
|
+
Buffer.from(signature, options.asymmetricSignatureEncoding),
|
|
752
|
+
);
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
export const safeEqual = (input, expected) => {
|
|
756
|
+
const bufferInput = Buffer.from(input);
|
|
757
|
+
const bufferExpected = Buffer.from(expected);
|
|
758
|
+
return (
|
|
759
|
+
bufferInput.length === bufferExpected.length &&
|
|
760
|
+
timingSafeEqual(bufferInput, bufferExpected)
|
|
761
|
+
);
|
|
762
|
+
};
|
package/package.json
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
2
|
+
"name": "@1auth/crypto",
|
|
3
|
+
"version": "0.0.0-beta.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=24"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"module": "./index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"default": "./index.js"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"types": "index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"index.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "npm run test:unit",
|
|
30
|
+
"test:unit": "node --test"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"funding": {
|
|
34
|
+
"type": "github",
|
|
35
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [],
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "1auth contributors",
|
|
40
|
+
"url": "https://github.com/willfarrell/1auth/graphs/contributors"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/willfarrell/1auth.git",
|
|
45
|
+
"directory": "packages/crypto"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/willfarrell/1auth/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/willfarrell/1auth",
|
|
51
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"nanoid": "5.1.6"
|
|
54
|
+
}
|
|
55
55
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 will Farrell
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|