@1auth/crypto 0.0.0-alpha.40 → 0.0.0-alpha.41
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 +46 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,6 +4,8 @@ import {
|
|
|
4
4
|
createHash as checksum,
|
|
5
5
|
createCipheriv,
|
|
6
6
|
createDecipheriv,
|
|
7
|
+
createHmac,
|
|
8
|
+
timingSafeEqual,
|
|
7
9
|
generateKeyPair as generateKeyPairCallback,
|
|
8
10
|
sign as signCallback,
|
|
9
11
|
verify as verifyCallback
|
|
@@ -22,6 +24,7 @@ const defaults = {
|
|
|
22
24
|
symetricEncryptionEncoding: 'base64', // https://nodejs.org/api/buffer.html#buffers-and-character-encodings
|
|
23
25
|
asymetricKey: 'P-384',
|
|
24
26
|
digestAlgorithm: 'sha3-384',
|
|
27
|
+
hmacAlgorithm: 'sha256',
|
|
25
28
|
signatureEncoding: 'base64'
|
|
26
29
|
}
|
|
27
30
|
const symetricEncryptionEncodingLengths = {}
|
|
@@ -379,13 +382,17 @@ export const makeAsymmetricKeys = async () => {
|
|
|
379
382
|
return { publicKey, privateKey }
|
|
380
383
|
}
|
|
381
384
|
|
|
382
|
-
export const
|
|
385
|
+
export const makeAsymmetricSignature = async (
|
|
386
|
+
data,
|
|
387
|
+
privateKey,
|
|
388
|
+
{ algorithm } = {}
|
|
389
|
+
) => {
|
|
383
390
|
algorithm ??= options.digestAlgorithm
|
|
384
391
|
return (await sign(algorithm, Buffer.from(data), privateKey)).toString(
|
|
385
392
|
options.signatureEncoding
|
|
386
393
|
)
|
|
387
394
|
}
|
|
388
|
-
export const
|
|
395
|
+
export const verifyAsymmetricSignature = async (
|
|
389
396
|
data,
|
|
390
397
|
publicKey,
|
|
391
398
|
signature,
|
|
@@ -399,3 +406,40 @@ export const verifySignature = async (
|
|
|
399
406
|
Buffer.from(signature, options.signatureEncoding)
|
|
400
407
|
)
|
|
401
408
|
}
|
|
409
|
+
|
|
410
|
+
export const makeSymmetricSignature = (
|
|
411
|
+
data,
|
|
412
|
+
encryptionKey,
|
|
413
|
+
{ algorithm } = {}
|
|
414
|
+
) => {
|
|
415
|
+
encryptionKey ??= options.symetricEncryptionKey
|
|
416
|
+
algorithm ??= options.hmacAlgorithm
|
|
417
|
+
const signature = createHmac('sha256', encryptionKey)
|
|
418
|
+
.update(data)
|
|
419
|
+
.digest('base64')
|
|
420
|
+
.replace(/=+$/, '')
|
|
421
|
+
const signedData = data + '.' + signature
|
|
422
|
+
return signedData
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export const verifySymmetricSignature = (
|
|
426
|
+
signedData,
|
|
427
|
+
encryptionKey,
|
|
428
|
+
{ algorithm } = {}
|
|
429
|
+
) => {
|
|
430
|
+
const data = signedData.slice(0, signedData.lastIndexOf('.'))
|
|
431
|
+
const signedDataExpected = makeSymmetricSignature(data, encryptionKey, {
|
|
432
|
+
algorithm
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
return safeEqual(signedData, signedDataExpected) && data
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export const safeEqual = (input, expected) => {
|
|
439
|
+
const bufferInput = Buffer.from(input)
|
|
440
|
+
const bufferExpected = Buffer.from(expected)
|
|
441
|
+
return (
|
|
442
|
+
bufferInput.length === bufferExpected.length &&
|
|
443
|
+
timingSafeEqual(bufferInput, bufferExpected)
|
|
444
|
+
)
|
|
445
|
+
}
|