@bearei/server-common 0.0.218 → 0.0.219

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.
@@ -1,3 +1,5 @@
1
- export declare const buildSignatureString: (object: Record<string, string | number>) => string;
2
- export declare const sign: (saltRounds?: number) => (object: Record<string, string>) => Promise<string>;
3
- export declare const signCompare: (data: Record<string, string> | string) => (hash: string) => Promise<boolean>;
1
+ export declare const buildCanonicalString: (object: Record<string, string | number>) => string;
2
+ export declare const createHmacSigner: (secret: string) => (data: Record<string, string>) => string;
3
+ export declare const createHmacVerifier: (secret: string) => (data: Record<string, string>) => (signature: string) => boolean;
4
+ export declare const createHashSigner: (saltRounds?: number) => (data: Record<string, string>) => Promise<string>;
5
+ export declare const verifyHashSignature: (data: Record<string, string> | string) => (hash: string) => Promise<boolean>;
@@ -23,31 +23,45 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.signCompare = exports.sign = exports.buildSignatureString = void 0;
26
+ exports.verifyHashSignature = exports.createHashSigner = exports.createHmacVerifier = exports.createHmacSigner = exports.buildCanonicalString = void 0;
27
27
  const bcrypt = __importStar(require("bcrypt"));
28
+ const crypto = __importStar(require("crypto"));
28
29
  const exception_1 = require("../../exception");
29
- const buildSignatureString = (object) => {
30
+ const buildCanonicalString = (object) => {
30
31
  const signString = Object.entries(object)
31
32
  .sort((a, b) => a[0].toLowerCase().localeCompare(b[0].toLowerCase()))
32
33
  .reduce((accumulator, [_, value]) => (value ? `${accumulator}&${value}` : accumulator), '');
33
- if (signString.length > 56) {
34
+ return signString;
35
+ };
36
+ exports.buildCanonicalString = buildCanonicalString;
37
+ const createHmacSigner = (secret) => {
38
+ return (data) => {
39
+ const message = (0, exports.buildCanonicalString)(data);
40
+ return crypto.createHmac('sha256', secret).update(message).digest('hex');
41
+ };
42
+ };
43
+ exports.createHmacSigner = createHmacSigner;
44
+ const createHmacVerifier = (secret) => {
45
+ const signer = (0, exports.createHmacSigner)(secret);
46
+ return (data) => (signature) => signer(data) === signature;
47
+ };
48
+ exports.createHmacVerifier = createHmacVerifier;
49
+ const createHashSigner = (saltRounds = 10) => async (data) => {
50
+ const str = (0, exports.buildCanonicalString)(data);
51
+ const byteLength = Buffer.byteLength(str, 'utf8');
52
+ if (byteLength > 72) {
34
53
  (0, exception_1.throwHTTPException)({
35
54
  code: exception_1.HTTP_EXCEPTION_CODE.SIGNATURE_STRING_LENGTH_LIMIT,
36
- message: 'The maximum length of the signature string should be 56 characters.'
55
+ message: 'Input string exceeds bcrypt maximum length of 72 bytes.'
37
56
  })(exception_1.HTTP_EXCEPTION.BAD_REQUEST);
38
57
  }
39
- return signString;
40
- };
41
- exports.buildSignatureString = buildSignatureString;
42
- const sign = (saltRounds = 10) => async (object) => {
43
- const sign = (0, exports.buildSignatureString)(object);
44
58
  const salt = await bcrypt.genSalt(saltRounds);
45
- return bcrypt.hash(sign, salt);
59
+ return bcrypt.hash(str, salt);
46
60
  };
47
- exports.sign = sign;
48
- const signCompare = (data) => {
49
- const sign = typeof data === 'string' ? data : (0, exports.buildSignatureString)(data);
50
- return async (hash) => bcrypt.compare(sign, hash);
61
+ exports.createHashSigner = createHashSigner;
62
+ const verifyHashSignature = (data) => {
63
+ const str = typeof data === 'string' ? data : (0, exports.buildCanonicalString)(data);
64
+ return async (hash) => bcrypt.compare(str, hash);
51
65
  };
52
- exports.signCompare = signCompare;
66
+ exports.verifyHashSignature = verifyHashSignature;
53
67
  //# sourceMappingURL=sign.utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sign.utils.js","sourceRoot":"","sources":["../../../src/utils/sign/sign.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAgC;AAChC,+CAAuF;AAEhF,MAAM,oBAAoB,GAAG,CAAC,MAAuC,EAAE,EAAE;IAC/E,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,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"@bearei/server-common",
3
- "version": "0.0.218",
3
+ "version": "0.0.219",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "bearei <bear_ei@outlook.com>",