@1auth/crypto 0.0.0-alpha.40 → 0.0.0-alpha.42

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 (2) hide show
  1. package/index.js +48 -2
  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 makeSignature = async (data, privateKey, { algorithm } = {}) => {
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 verifySignature = async (
395
+ export const verifyAsymmetricSignature = async (
389
396
  data,
390
397
  publicKey,
391
398
  signature,
@@ -399,3 +406,42 @@ 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
+ // if (typeof data !== 'string') throw new TypeError('data must me a string')
416
+ encryptionKey ??= options.symetricEncryptionKey
417
+ algorithm ??= options.hmacAlgorithm
418
+ const signature = createHmac('sha256', encryptionKey)
419
+ .update(data)
420
+ .digest('base64')
421
+ .replace(/=+$/, '')
422
+ const signedData = data + '.' + signature
423
+ return signedData
424
+ }
425
+
426
+ export const verifySymmetricSignature = (
427
+ signedData,
428
+ encryptionKey,
429
+ { algorithm } = {}
430
+ ) => {
431
+ if (typeof signedData !== 'string') return false
432
+ const data = signedData.slice(0, signedData.lastIndexOf('.'))
433
+ const signedDataExpected = makeSymmetricSignature(data, encryptionKey, {
434
+ algorithm
435
+ })
436
+
437
+ return safeEqual(signedData, signedDataExpected) && data
438
+ }
439
+
440
+ export const safeEqual = (input, expected) => {
441
+ const bufferInput = Buffer.from(input)
442
+ const bufferExpected = Buffer.from(expected)
443
+ return (
444
+ bufferInput.length === bufferExpected.length &&
445
+ timingSafeEqual(bufferInput, bufferExpected)
446
+ )
447
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/crypto",
3
- "version": "0.0.0-alpha.40",
3
+ "version": "0.0.0-alpha.42",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {