@depup/aws-sdk__checksums 3.1000.18-depup.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/LICENSE +201 -0
- package/README.md +32 -0
- package/changes.json +14 -0
- package/crc.d.ts +5 -0
- package/crc.js +5 -0
- package/dist-cjs/index.js +22 -0
- package/dist-cjs/submodules/crc/index.js +159 -0
- package/dist-cjs/submodules/flexible-checksums/index.js +444 -0
- package/dist-cjs/submodules/md5/index.js +4 -0
- package/dist-cjs/submodules/sha/index.js +312 -0
- package/dist-es/index.js +2 -0
- package/dist-es/submodules/crc/crc32c/Crc32cJs.js +31 -0
- package/dist-es/submodules/crc/crc32c/Crc32cNode.js +2 -0
- package/dist-es/submodules/crc/crc64-nvme/Crc64Nvme.js +18 -0
- package/dist-es/submodules/crc/crc64-nvme/Crc64NvmeJs.js +92 -0
- package/dist-es/submodules/crc/crc64-nvme/crc64-nvme-crt-container.js +3 -0
- package/dist-es/submodules/crc/index.browser.js +6 -0
- package/dist-es/submodules/crc/index.js +6 -0
- package/dist-es/submodules/flexible-checksums/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.js +9 -0
- package/dist-es/submodules/flexible-checksums/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.js +9 -0
- package/dist-es/submodules/flexible-checksums/configuration.js +1 -0
- package/dist-es/submodules/flexible-checksums/constants.js +25 -0
- package/dist-es/submodules/flexible-checksums/flexibleChecksumsInputMiddleware.js +37 -0
- package/dist-es/submodules/flexible-checksums/flexibleChecksumsMiddleware.js +123 -0
- package/dist-es/submodules/flexible-checksums/flexibleChecksumsResponseMiddleware.js +48 -0
- package/dist-es/submodules/flexible-checksums/getChecksum.js +2 -0
- package/dist-es/submodules/flexible-checksums/getChecksumAlgorithmForRequest.js +13 -0
- package/dist-es/submodules/flexible-checksums/getChecksumAlgorithmListForResponse.js +15 -0
- package/dist-es/submodules/flexible-checksums/getChecksumLocationName.js +2 -0
- package/dist-es/submodules/flexible-checksums/getFlexibleChecksumsPlugin.js +10 -0
- package/dist-es/submodules/flexible-checksums/hasHeader.js +9 -0
- package/dist-es/submodules/flexible-checksums/hasHeaderWithPrefix.js +9 -0
- package/dist-es/submodules/flexible-checksums/index.js +6 -0
- package/dist-es/submodules/flexible-checksums/isChecksumWithPartNumber.js +13 -0
- package/dist-es/submodules/flexible-checksums/isStreaming.js +2 -0
- package/dist-es/submodules/flexible-checksums/resolveFlexibleChecksumsConfig.js +11 -0
- package/dist-es/submodules/flexible-checksums/selectChecksumAlgorithmFunction.js +27 -0
- package/dist-es/submodules/flexible-checksums/stringHasher.js +6 -0
- package/dist-es/submodules/flexible-checksums/stringUnionSelector.js +14 -0
- package/dist-es/submodules/flexible-checksums/types.js +15 -0
- package/dist-es/submodules/flexible-checksums/validateChecksumFromResponse.js +45 -0
- package/dist-es/submodules/md5/index.js +1 -0
- package/dist-es/submodules/sha/index.browser.js +4 -0
- package/dist-es/submodules/sha/index.js +4 -0
- package/dist-es/submodules/sha/sha1/Sha1Js.js +173 -0
- package/dist-es/submodules/sha/sha1/Sha1Node.js +59 -0
- package/dist-es/submodules/sha/sha1/Sha1WebCrypto.js +71 -0
- package/dist-types/index.d.ts +3 -0
- package/dist-types/submodules/crc/crc32c/Crc32cJs.d.ts +13 -0
- package/dist-types/submodules/crc/crc32c/Crc32cNode.d.ts +13 -0
- package/dist-types/submodules/crc/crc64-nvme/Crc64Nvme.d.ts +21 -0
- package/dist-types/submodules/crc/crc64-nvme/Crc64NvmeJs.d.ts +14 -0
- package/dist-types/submodules/crc/crc64-nvme/crc64-nvme-crt-container.d.ts +11 -0
- package/dist-types/submodules/crc/index.browser.d.ts +5 -0
- package/dist-types/submodules/crc/index.d.ts +6 -0
- package/dist-types/submodules/flexible-checksums/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.d.ts +14 -0
- package/dist-types/submodules/flexible-checksums/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.d.ts +14 -0
- package/dist-types/submodules/flexible-checksums/configuration.d.ts +58 -0
- package/dist-types/submodules/flexible-checksums/constants.d.ts +83 -0
- package/dist-types/submodules/flexible-checksums/flexibleChecksumsInputMiddleware.d.ts +22 -0
- package/dist-types/submodules/flexible-checksums/flexibleChecksumsMiddleware.d.ts +33 -0
- package/dist-types/submodules/flexible-checksums/flexibleChecksumsResponseMiddleware.d.ts +27 -0
- package/dist-types/submodules/flexible-checksums/getChecksum.d.ts +6 -0
- package/dist-types/submodules/flexible-checksums/getChecksumAlgorithmForRequest.d.ts +22 -0
- package/dist-types/submodules/flexible-checksums/getChecksumAlgorithmListForResponse.d.ts +6 -0
- package/dist-types/submodules/flexible-checksums/getChecksumLocationName.d.ts +5 -0
- package/dist-types/submodules/flexible-checksums/getFlexibleChecksumsPlugin.d.ts +14 -0
- package/dist-types/submodules/flexible-checksums/hasHeader.d.ts +6 -0
- package/dist-types/submodules/flexible-checksums/hasHeaderWithPrefix.d.ts +6 -0
- package/dist-types/submodules/flexible-checksums/index.d.ts +9 -0
- package/dist-types/submodules/flexible-checksums/isChecksumWithPartNumber.d.ts +1 -0
- package/dist-types/submodules/flexible-checksums/isStreaming.d.ts +4 -0
- package/dist-types/submodules/flexible-checksums/resolveFlexibleChecksumsConfig.d.ts +57 -0
- package/dist-types/submodules/flexible-checksums/selectChecksumAlgorithmFunction.d.ts +7 -0
- package/dist-types/submodules/flexible-checksums/stringHasher.d.ts +5 -0
- package/dist-types/submodules/flexible-checksums/stringUnionSelector.d.ts +12 -0
- package/dist-types/submodules/flexible-checksums/types.d.ts +9 -0
- package/dist-types/submodules/flexible-checksums/validateChecksumFromResponse.d.ts +13 -0
- package/dist-types/submodules/md5/index.d.ts +1 -0
- package/dist-types/submodules/sha/index.browser.d.ts +3 -0
- package/dist-types/submodules/sha/index.d.ts +4 -0
- package/dist-types/submodules/sha/sha1/Sha1Js.d.ts +26 -0
- package/dist-types/submodules/sha/sha1/Sha1Node.d.ts +13 -0
- package/dist-types/submodules/sha/sha1/Sha1WebCrypto.d.ts +22 -0
- package/dist-types/ts3.4/index.d.ts +26 -0
- package/dist-types/ts3.4/submodules/crc/crc32c/Crc32cJs.d.ts +8 -0
- package/dist-types/ts3.4/submodules/crc/crc32c/Crc32cNode.d.ts +5 -0
- package/dist-types/ts3.4/submodules/crc/crc64-nvme/Crc64Nvme.d.ts +8 -0
- package/dist-types/ts3.4/submodules/crc/crc64-nvme/Crc64NvmeJs.d.ts +9 -0
- package/dist-types/ts3.4/submodules/crc/crc64-nvme/crc64-nvme-crt-container.d.ts +4 -0
- package/dist-types/ts3.4/submodules/crc/index.browser.d.ts +5 -0
- package/dist-types/ts3.4/submodules/crc/index.d.ts +6 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.d.ts +5 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.d.ts +5 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/configuration.d.ts +26 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/constants.d.ts +27 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/flexibleChecksumsInputMiddleware.d.ts +10 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/flexibleChecksumsMiddleware.d.ts +14 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/flexibleChecksumsResponseMiddleware.d.ts +11 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/getChecksum.d.ts +9 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/getChecksumAlgorithmForRequest.d.ts +15 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/getChecksumAlgorithmListForResponse.d.ts +4 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/getChecksumLocationName.d.ts +2 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/getFlexibleChecksumsPlugin.d.ts +14 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/hasHeader.d.ts +2 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/hasHeaderWithPrefix.d.ts +2 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/index.d.ts +31 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/isChecksumWithPartNumber.d.ts +1 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/isStreaming.d.ts +1 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/resolveFlexibleChecksumsConfig.d.ts +25 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/selectChecksumAlgorithmFunction.d.ts +7 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/stringHasher.d.ts +5 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/stringUnionSelector.d.ts +10 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/types.d.ts +3 -0
- package/dist-types/ts3.4/submodules/flexible-checksums/validateChecksumFromResponse.d.ts +12 -0
- package/dist-types/ts3.4/submodules/md5/index.d.ts +1 -0
- package/dist-types/ts3.4/submodules/sha/index.browser.d.ts +3 -0
- package/dist-types/ts3.4/submodules/sha/index.d.ts +4 -0
- package/dist-types/ts3.4/submodules/sha/sha1/Sha1Js.d.ts +20 -0
- package/dist-types/ts3.4/submodules/sha/sha1/Sha1Node.d.ts +5 -0
- package/dist-types/ts3.4/submodules/sha/sha1/Sha1WebCrypto.d.ts +14 -0
- package/flexible-checksums.d.ts +5 -0
- package/flexible-checksums.js +5 -0
- package/md5.d.ts +5 -0
- package/md5.js +5 -0
- package/package.json +159 -0
- package/sha.d.ts +5 -0
- package/sha.js +5 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { setFeature } from "@aws-sdk/core/client";
|
|
2
|
+
import { HttpRequest } from "@smithy/core/protocols";
|
|
3
|
+
import { createBufferedReadable } from "@smithy/core/serde";
|
|
4
|
+
import { ChecksumAlgorithm, DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation } from "./constants";
|
|
5
|
+
import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest";
|
|
6
|
+
import { getChecksumLocationName } from "./getChecksumLocationName";
|
|
7
|
+
import { hasHeader } from "./hasHeader";
|
|
8
|
+
import { hasHeaderWithPrefix } from "./hasHeaderWithPrefix";
|
|
9
|
+
import { isStreaming } from "./isStreaming";
|
|
10
|
+
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";
|
|
11
|
+
import { stringHasher } from "./stringHasher";
|
|
12
|
+
export const flexibleChecksumsMiddlewareOptions = {
|
|
13
|
+
name: "flexibleChecksumsMiddleware",
|
|
14
|
+
step: "build",
|
|
15
|
+
tags: ["BODY_CHECKSUM"],
|
|
16
|
+
override: true,
|
|
17
|
+
};
|
|
18
|
+
export const flexibleChecksumsMiddleware = (config, middlewareConfig) => (next, context) => async (args) => {
|
|
19
|
+
if (!HttpRequest.isInstance(args.request)) {
|
|
20
|
+
return next(args);
|
|
21
|
+
}
|
|
22
|
+
if (hasHeaderWithPrefix("x-amz-checksum-", args.request.headers)) {
|
|
23
|
+
return next(args);
|
|
24
|
+
}
|
|
25
|
+
const { request, input } = args;
|
|
26
|
+
const { body: requestBody, headers } = request;
|
|
27
|
+
const { base64Encoder, streamHasher } = config;
|
|
28
|
+
const { requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;
|
|
29
|
+
const requestChecksumCalculation = await config.requestChecksumCalculation();
|
|
30
|
+
const requestAlgorithmMemberName = requestAlgorithmMember?.name;
|
|
31
|
+
const requestAlgorithmMemberHttpHeader = requestAlgorithmMember?.httpHeader;
|
|
32
|
+
if (requestAlgorithmMemberName && !input[requestAlgorithmMemberName]) {
|
|
33
|
+
if (requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired) {
|
|
34
|
+
input[requestAlgorithmMemberName] = DEFAULT_CHECKSUM_ALGORITHM;
|
|
35
|
+
if (requestAlgorithmMemberHttpHeader) {
|
|
36
|
+
headers[requestAlgorithmMemberHttpHeader] = DEFAULT_CHECKSUM_ALGORITHM;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const checksumAlgorithm = getChecksumAlgorithmForRequest(input, {
|
|
41
|
+
requestChecksumRequired,
|
|
42
|
+
requestAlgorithmMember: requestAlgorithmMember?.name,
|
|
43
|
+
requestChecksumCalculation,
|
|
44
|
+
});
|
|
45
|
+
let updatedBody = requestBody;
|
|
46
|
+
let updatedHeaders = headers;
|
|
47
|
+
if (checksumAlgorithm) {
|
|
48
|
+
switch (checksumAlgorithm) {
|
|
49
|
+
case ChecksumAlgorithm.CRC32:
|
|
50
|
+
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_CRC32", "U");
|
|
51
|
+
break;
|
|
52
|
+
case ChecksumAlgorithm.CRC32C:
|
|
53
|
+
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_CRC32C", "V");
|
|
54
|
+
break;
|
|
55
|
+
case ChecksumAlgorithm.CRC64NVME:
|
|
56
|
+
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_CRC64", "W");
|
|
57
|
+
break;
|
|
58
|
+
case ChecksumAlgorithm.SHA1:
|
|
59
|
+
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_SHA1", "X");
|
|
60
|
+
break;
|
|
61
|
+
case ChecksumAlgorithm.SHA256:
|
|
62
|
+
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_SHA256", "Y");
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
const checksumLocationName = getChecksumLocationName(checksumAlgorithm);
|
|
66
|
+
const checksumAlgorithmFn = selectChecksumAlgorithmFunction(checksumAlgorithm, config);
|
|
67
|
+
if (isStreaming(requestBody)) {
|
|
68
|
+
const { getAwsChunkedEncodingStream, bodyLengthChecker } = config;
|
|
69
|
+
updatedBody = getAwsChunkedEncodingStream(typeof config.requestStreamBufferSize === "number" && config.requestStreamBufferSize >= 8 * 1024
|
|
70
|
+
? createBufferedReadable(requestBody, config.requestStreamBufferSize, context.logger)
|
|
71
|
+
: requestBody, {
|
|
72
|
+
base64Encoder,
|
|
73
|
+
bodyLengthChecker,
|
|
74
|
+
checksumLocationName,
|
|
75
|
+
checksumAlgorithmFn,
|
|
76
|
+
streamHasher,
|
|
77
|
+
});
|
|
78
|
+
updatedHeaders = {
|
|
79
|
+
...headers,
|
|
80
|
+
"content-encoding": headers["content-encoding"]
|
|
81
|
+
? `${headers["content-encoding"]},aws-chunked`
|
|
82
|
+
: "aws-chunked",
|
|
83
|
+
"transfer-encoding": "chunked",
|
|
84
|
+
"x-amz-decoded-content-length": headers["content-length"],
|
|
85
|
+
"x-amz-content-sha256": "STREAMING-UNSIGNED-PAYLOAD-TRAILER",
|
|
86
|
+
"x-amz-trailer": checksumLocationName,
|
|
87
|
+
};
|
|
88
|
+
delete updatedHeaders["content-length"];
|
|
89
|
+
}
|
|
90
|
+
else if (!hasHeader(checksumLocationName, headers)) {
|
|
91
|
+
const rawChecksum = await stringHasher(checksumAlgorithmFn, requestBody);
|
|
92
|
+
updatedHeaders = {
|
|
93
|
+
...headers,
|
|
94
|
+
[checksumLocationName]: base64Encoder(rawChecksum),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const result = await next({
|
|
100
|
+
...args,
|
|
101
|
+
request: {
|
|
102
|
+
...request,
|
|
103
|
+
headers: updatedHeaders,
|
|
104
|
+
body: updatedBody,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (e instanceof Error && e.name === "InvalidChunkSizeError") {
|
|
111
|
+
try {
|
|
112
|
+
if (!e.message.endsWith(".")) {
|
|
113
|
+
e.message += ".";
|
|
114
|
+
}
|
|
115
|
+
e.message +=
|
|
116
|
+
" Set [requestStreamBufferSize=number e.g. 65_536] in client constructor to instruct AWS SDK to buffer your input stream.";
|
|
117
|
+
}
|
|
118
|
+
catch (ignored) {
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
throw e;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { HttpRequest } from "@smithy/core/protocols";
|
|
2
|
+
import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListForResponse";
|
|
3
|
+
import { getChecksumLocationName } from "./getChecksumLocationName";
|
|
4
|
+
import { isChecksumWithPartNumber } from "./isChecksumWithPartNumber";
|
|
5
|
+
import { validateChecksumFromResponse } from "./validateChecksumFromResponse";
|
|
6
|
+
export const flexibleChecksumsResponseMiddlewareOptions = {
|
|
7
|
+
name: "flexibleChecksumsResponseMiddleware",
|
|
8
|
+
toMiddleware: "deserializerMiddleware",
|
|
9
|
+
relation: "after",
|
|
10
|
+
tags: ["BODY_CHECKSUM"],
|
|
11
|
+
override: true,
|
|
12
|
+
};
|
|
13
|
+
export const flexibleChecksumsResponseMiddleware = (config, middlewareConfig) => (next, context) => async (args) => {
|
|
14
|
+
if (!HttpRequest.isInstance(args.request)) {
|
|
15
|
+
return next(args);
|
|
16
|
+
}
|
|
17
|
+
const input = args.input;
|
|
18
|
+
const result = await next(args);
|
|
19
|
+
const response = result.response;
|
|
20
|
+
const { requestValidationModeMember, responseAlgorithms } = middlewareConfig;
|
|
21
|
+
if (requestValidationModeMember && input[requestValidationModeMember] === "ENABLED") {
|
|
22
|
+
const { clientName, commandName } = context;
|
|
23
|
+
const customChecksumAlgorithms = Object.keys(config.checksumAlgorithms ?? {}).filter((algorithm) => {
|
|
24
|
+
const responseHeader = getChecksumLocationName(algorithm);
|
|
25
|
+
return response.headers[responseHeader] !== undefined;
|
|
26
|
+
});
|
|
27
|
+
const algoList = getChecksumAlgorithmListForResponse([
|
|
28
|
+
...(responseAlgorithms ?? []),
|
|
29
|
+
...customChecksumAlgorithms,
|
|
30
|
+
]);
|
|
31
|
+
const isS3WholeObjectMultipartGetResponseChecksum = clientName === "S3Client" &&
|
|
32
|
+
commandName === "GetObjectCommand" &&
|
|
33
|
+
algoList.every((algorithm) => {
|
|
34
|
+
const responseHeader = getChecksumLocationName(algorithm);
|
|
35
|
+
const checksumFromResponse = response.headers[responseHeader];
|
|
36
|
+
return !checksumFromResponse || isChecksumWithPartNumber(checksumFromResponse);
|
|
37
|
+
});
|
|
38
|
+
if (isS3WholeObjectMultipartGetResponseChecksum) {
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
await validateChecksumFromResponse(response, {
|
|
42
|
+
config,
|
|
43
|
+
responseAlgorithms: algoList,
|
|
44
|
+
logger: context.logger,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation } from "./constants";
|
|
2
|
+
export const getChecksumAlgorithmForRequest = (input, { requestChecksumRequired, requestAlgorithmMember, requestChecksumCalculation }) => {
|
|
3
|
+
if (!requestAlgorithmMember) {
|
|
4
|
+
return requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired
|
|
5
|
+
? DEFAULT_CHECKSUM_ALGORITHM
|
|
6
|
+
: undefined;
|
|
7
|
+
}
|
|
8
|
+
if (!input[requestAlgorithmMember]) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const checksumAlgorithm = input[requestAlgorithmMember];
|
|
12
|
+
return checksumAlgorithm;
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PRIORITY_ORDER_ALGORITHMS } from "./types";
|
|
2
|
+
export const getChecksumAlgorithmListForResponse = (responseAlgorithms = []) => {
|
|
3
|
+
const validChecksumAlgorithms = [];
|
|
4
|
+
let i = PRIORITY_ORDER_ALGORITHMS.length;
|
|
5
|
+
for (const algorithm of responseAlgorithms) {
|
|
6
|
+
const priority = PRIORITY_ORDER_ALGORITHMS.indexOf(algorithm);
|
|
7
|
+
if (priority !== -1) {
|
|
8
|
+
validChecksumAlgorithms[priority] = algorithm;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
validChecksumAlgorithms[i++] = algorithm;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return validChecksumAlgorithms.filter(Boolean);
|
|
15
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { flexibleChecksumsInputMiddleware, flexibleChecksumsInputMiddlewareOptions, } from "./flexibleChecksumsInputMiddleware";
|
|
2
|
+
import { flexibleChecksumsMiddleware, flexibleChecksumsMiddlewareOptions } from "./flexibleChecksumsMiddleware";
|
|
3
|
+
import { flexibleChecksumsResponseMiddleware, flexibleChecksumsResponseMiddlewareOptions, } from "./flexibleChecksumsResponseMiddleware";
|
|
4
|
+
export const getFlexibleChecksumsPlugin = (config, middlewareConfig) => ({
|
|
5
|
+
applyToStack: (clientStack) => {
|
|
6
|
+
clientStack.add(flexibleChecksumsMiddleware(config, middlewareConfig), flexibleChecksumsMiddlewareOptions);
|
|
7
|
+
clientStack.addRelativeTo(flexibleChecksumsInputMiddleware(config, middlewareConfig), flexibleChecksumsInputMiddlewareOptions);
|
|
8
|
+
clientStack.addRelativeTo(flexibleChecksumsResponseMiddleware(config, middlewareConfig), flexibleChecksumsResponseMiddlewareOptions);
|
|
9
|
+
},
|
|
10
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const hasHeaderWithPrefix = (headerPrefix, headers) => {
|
|
2
|
+
const soughtHeaderPrefix = headerPrefix.toLowerCase();
|
|
3
|
+
for (const headerName of Object.keys(headers)) {
|
|
4
|
+
if (headerName.toLowerCase().startsWith(soughtHeaderPrefix)) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ENV_REQUEST_CHECKSUM_CALCULATION, CONFIG_REQUEST_CHECKSUM_CALCULATION, NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS, } from "./NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS";
|
|
2
|
+
export { ENV_RESPONSE_CHECKSUM_VALIDATION, CONFIG_RESPONSE_CHECKSUM_VALIDATION, NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS, } from "./NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS";
|
|
3
|
+
export { RequestChecksumCalculation, DEFAULT_REQUEST_CHECKSUM_CALCULATION, ResponseChecksumValidation, DEFAULT_RESPONSE_CHECKSUM_VALIDATION, ChecksumAlgorithm, ChecksumLocation, DEFAULT_CHECKSUM_ALGORITHM, } from "./constants";
|
|
4
|
+
export { flexibleChecksumsMiddlewareOptions, flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware";
|
|
5
|
+
export { getFlexibleChecksumsPlugin } from "./getFlexibleChecksumsPlugin";
|
|
6
|
+
export { resolveFlexibleChecksumsConfig } from "./resolveFlexibleChecksumsConfig";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const isChecksumWithPartNumber = (checksum) => {
|
|
2
|
+
const lastHyphenIndex = checksum.lastIndexOf("-");
|
|
3
|
+
if (lastHyphenIndex !== -1) {
|
|
4
|
+
const numberPart = checksum.slice(lastHyphenIndex + 1);
|
|
5
|
+
if (!numberPart.startsWith("0")) {
|
|
6
|
+
const number = parseInt(numberPart, 10);
|
|
7
|
+
if (!isNaN(number) && number >= 1 && number <= 10000) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { normalizeProvider } from "@smithy/core/client";
|
|
2
|
+
import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, DEFAULT_RESPONSE_CHECKSUM_VALIDATION } from "./constants";
|
|
3
|
+
export const resolveFlexibleChecksumsConfig = (input) => {
|
|
4
|
+
const { requestChecksumCalculation, responseChecksumValidation, requestStreamBufferSize } = input;
|
|
5
|
+
return Object.assign(input, {
|
|
6
|
+
requestChecksumCalculation: normalizeProvider(requestChecksumCalculation ?? DEFAULT_REQUEST_CHECKSUM_CALCULATION),
|
|
7
|
+
responseChecksumValidation: normalizeProvider(responseChecksumValidation ?? DEFAULT_RESPONSE_CHECKSUM_VALIDATION),
|
|
8
|
+
requestStreamBufferSize: Number(requestStreamBufferSize ?? 0),
|
|
9
|
+
checksumAlgorithms: input.checksumAlgorithms ?? {},
|
|
10
|
+
});
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Crc32, Crc32c, Crc64Nvme } from "@aws-sdk/checksums/crc";
|
|
2
|
+
import { ChecksumAlgorithm } from "./constants";
|
|
3
|
+
import { CLIENT_SUPPORTED_ALGORITHMS } from "./types";
|
|
4
|
+
export const selectChecksumAlgorithmFunction = (checksumAlgorithm, config) => {
|
|
5
|
+
const { checksumAlgorithms = {} } = config;
|
|
6
|
+
switch (checksumAlgorithm) {
|
|
7
|
+
case ChecksumAlgorithm.MD5:
|
|
8
|
+
return checksumAlgorithms?.MD5 ?? config.md5;
|
|
9
|
+
case ChecksumAlgorithm.CRC32:
|
|
10
|
+
return checksumAlgorithms?.CRC32 ?? Crc32;
|
|
11
|
+
case ChecksumAlgorithm.CRC32C:
|
|
12
|
+
return checksumAlgorithms?.CRC32C ?? Crc32c;
|
|
13
|
+
case ChecksumAlgorithm.CRC64NVME:
|
|
14
|
+
return checksumAlgorithms?.CRC64NVME ?? Crc64Nvme;
|
|
15
|
+
case ChecksumAlgorithm.SHA1:
|
|
16
|
+
return checksumAlgorithms?.SHA1 ?? config.sha1;
|
|
17
|
+
case ChecksumAlgorithm.SHA256:
|
|
18
|
+
return checksumAlgorithms?.SHA256 ?? config.sha256;
|
|
19
|
+
default:
|
|
20
|
+
if (checksumAlgorithms?.[checksumAlgorithm]) {
|
|
21
|
+
return checksumAlgorithms[checksumAlgorithm];
|
|
22
|
+
}
|
|
23
|
+
throw new Error(`The checksum algorithm "${checksumAlgorithm}" is not supported by the client.` +
|
|
24
|
+
` Select one of ${CLIENT_SUPPORTED_ALGORITHMS}, or provide an implementation to ` +
|
|
25
|
+
` the client constructor checksums field.`);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var SelectorType;
|
|
2
|
+
(function (SelectorType) {
|
|
3
|
+
SelectorType["ENV"] = "env";
|
|
4
|
+
SelectorType["CONFIG"] = "shared config entry";
|
|
5
|
+
})(SelectorType || (SelectorType = {}));
|
|
6
|
+
export const stringUnionSelector = (obj, key, union, type) => {
|
|
7
|
+
if (!(key in obj))
|
|
8
|
+
return undefined;
|
|
9
|
+
const value = obj[key].toUpperCase();
|
|
10
|
+
if (!Object.values(union).includes(value)) {
|
|
11
|
+
throw new TypeError(`Cannot load ${type} '${key}'. Expected one of ${Object.values(union)}, got '${obj[key]}'.`);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ChecksumAlgorithm } from "./constants";
|
|
2
|
+
export const CLIENT_SUPPORTED_ALGORITHMS = [
|
|
3
|
+
ChecksumAlgorithm.CRC32,
|
|
4
|
+
ChecksumAlgorithm.CRC32C,
|
|
5
|
+
ChecksumAlgorithm.CRC64NVME,
|
|
6
|
+
ChecksumAlgorithm.SHA1,
|
|
7
|
+
ChecksumAlgorithm.SHA256,
|
|
8
|
+
];
|
|
9
|
+
export const PRIORITY_ORDER_ALGORITHMS = [
|
|
10
|
+
ChecksumAlgorithm.SHA256,
|
|
11
|
+
ChecksumAlgorithm.SHA1,
|
|
12
|
+
ChecksumAlgorithm.CRC32,
|
|
13
|
+
ChecksumAlgorithm.CRC32C,
|
|
14
|
+
ChecksumAlgorithm.CRC64NVME,
|
|
15
|
+
];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createChecksumStream } from "@smithy/core/serde";
|
|
2
|
+
import { ChecksumAlgorithm } from "./constants";
|
|
3
|
+
import { getChecksum } from "./getChecksum";
|
|
4
|
+
import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListForResponse";
|
|
5
|
+
import { getChecksumLocationName } from "./getChecksumLocationName";
|
|
6
|
+
import { isStreaming } from "./isStreaming";
|
|
7
|
+
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";
|
|
8
|
+
export const validateChecksumFromResponse = async (response, { config, responseAlgorithms, logger }) => {
|
|
9
|
+
const checksumAlgorithms = getChecksumAlgorithmListForResponse(responseAlgorithms);
|
|
10
|
+
const { body: responseBody, headers: responseHeaders } = response;
|
|
11
|
+
for (const algorithm of checksumAlgorithms) {
|
|
12
|
+
const responseHeader = getChecksumLocationName(algorithm);
|
|
13
|
+
const checksumFromResponse = responseHeaders[responseHeader];
|
|
14
|
+
if (checksumFromResponse) {
|
|
15
|
+
let checksumAlgorithmFn;
|
|
16
|
+
try {
|
|
17
|
+
checksumAlgorithmFn = selectChecksumAlgorithmFunction(algorithm, config);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (algorithm === ChecksumAlgorithm.CRC64NVME) {
|
|
21
|
+
logger?.warn(`Skipping ${ChecksumAlgorithm.CRC64NVME} checksum validation: ${error.message}`);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
const { base64Encoder } = config;
|
|
27
|
+
if (isStreaming(responseBody)) {
|
|
28
|
+
response.body = createChecksumStream({
|
|
29
|
+
expectedChecksum: checksumFromResponse,
|
|
30
|
+
checksumSourceLocation: responseHeader,
|
|
31
|
+
checksum: new checksumAlgorithmFn(),
|
|
32
|
+
source: responseBody,
|
|
33
|
+
base64Encoder,
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const checksum = await getChecksum(responseBody, { checksumAlgorithmFn, base64Encoder });
|
|
38
|
+
if (checksum === checksumFromResponse) {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Checksum mismatch: expected "${checksum}" but received "${checksumFromResponse}"` +
|
|
42
|
+
` in response header "${responseHeader}".`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Md5, Md5Js, Md5Node } from "@smithy/core/checksum";
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { toUint8Array } from "@smithy/core/serde";
|
|
2
|
+
const BLOCK = 64;
|
|
3
|
+
const DIGEST_LENGTH = 20;
|
|
4
|
+
const INIT = new Int32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
|
|
5
|
+
const K = new Int32Array([0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6]);
|
|
6
|
+
export class Sha1Js {
|
|
7
|
+
digestLength = DIGEST_LENGTH;
|
|
8
|
+
state = Int32Array.from(INIT);
|
|
9
|
+
w;
|
|
10
|
+
buffer = new Uint8Array(BLOCK);
|
|
11
|
+
bufferLength = 0;
|
|
12
|
+
bytesHashed = 0;
|
|
13
|
+
finished = false;
|
|
14
|
+
inner;
|
|
15
|
+
outer;
|
|
16
|
+
constructor(secret) {
|
|
17
|
+
if (secret) {
|
|
18
|
+
const key = Sha1Js.normalizeKey(secret);
|
|
19
|
+
this.inner = new Sha1Js();
|
|
20
|
+
this.outer = new Sha1Js();
|
|
21
|
+
const pad = new Uint8Array(BLOCK * 2);
|
|
22
|
+
for (let i = 0; i < BLOCK; ++i) {
|
|
23
|
+
pad[i] = 0x36 ^ key[i];
|
|
24
|
+
pad[i + BLOCK] = 0x5c ^ key[i];
|
|
25
|
+
}
|
|
26
|
+
this.inner.update(pad.subarray(0, BLOCK));
|
|
27
|
+
this.outer.update(pad.subarray(BLOCK));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
update(data) {
|
|
31
|
+
if (this.finished) {
|
|
32
|
+
throw new Error("Attempted to update an already finished HMAC.");
|
|
33
|
+
}
|
|
34
|
+
if (this.inner) {
|
|
35
|
+
this.inner.update(data);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
let pos = 0;
|
|
39
|
+
let { length } = data;
|
|
40
|
+
this.bytesHashed += length;
|
|
41
|
+
if (this.bufferLength > 0) {
|
|
42
|
+
while (length > 0 && this.bufferLength < BLOCK) {
|
|
43
|
+
this.buffer[this.bufferLength++] = data[pos++];
|
|
44
|
+
--length;
|
|
45
|
+
}
|
|
46
|
+
if (this.bufferLength === BLOCK) {
|
|
47
|
+
this.hashBuffer(this.buffer, 0);
|
|
48
|
+
this.bufferLength = 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
while (length >= BLOCK) {
|
|
52
|
+
this.hashBuffer(data, pos);
|
|
53
|
+
pos += BLOCK;
|
|
54
|
+
length -= BLOCK;
|
|
55
|
+
}
|
|
56
|
+
while (length > 0) {
|
|
57
|
+
this.buffer[this.bufferLength++] = data[pos++];
|
|
58
|
+
--length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async digest() {
|
|
62
|
+
if (this.inner && this.outer) {
|
|
63
|
+
if (this.finished) {
|
|
64
|
+
throw new Error("Attempted to digest an already finished HMAC.");
|
|
65
|
+
}
|
|
66
|
+
this.finished = true;
|
|
67
|
+
const innerDigest = this.inner.digestSync();
|
|
68
|
+
this.outer.update(innerDigest);
|
|
69
|
+
return this.outer.digestSync();
|
|
70
|
+
}
|
|
71
|
+
return this.digestSync();
|
|
72
|
+
}
|
|
73
|
+
reset() {
|
|
74
|
+
this.state = Int32Array.from(INIT);
|
|
75
|
+
this.buffer = new Uint8Array(BLOCK);
|
|
76
|
+
this.bufferLength = 0;
|
|
77
|
+
this.bytesHashed = 0;
|
|
78
|
+
}
|
|
79
|
+
digestSync() {
|
|
80
|
+
const state = this.state.slice();
|
|
81
|
+
const buffer = this.buffer.slice();
|
|
82
|
+
let bufferLength = this.bufferLength;
|
|
83
|
+
const bitsHi = (this.bytesHashed / 0x20000000) | 0;
|
|
84
|
+
const bitsLo = this.bytesHashed << 3;
|
|
85
|
+
buffer[bufferLength++] = 0x80;
|
|
86
|
+
if (bufferLength > BLOCK - 8) {
|
|
87
|
+
for (let i = bufferLength; i < BLOCK; ++i) {
|
|
88
|
+
buffer[i] = 0;
|
|
89
|
+
}
|
|
90
|
+
this.hashBufferWith(state, buffer, 0);
|
|
91
|
+
bufferLength = 0;
|
|
92
|
+
}
|
|
93
|
+
for (let i = bufferLength; i < BLOCK - 8; ++i) {
|
|
94
|
+
buffer[i] = 0;
|
|
95
|
+
}
|
|
96
|
+
const v = new DataView(buffer.buffer, buffer.byteOffset, BLOCK);
|
|
97
|
+
v.setUint32(BLOCK - 8, bitsHi, false);
|
|
98
|
+
v.setUint32(BLOCK - 4, bitsLo, false);
|
|
99
|
+
this.hashBufferWith(state, buffer, 0);
|
|
100
|
+
const out = new Uint8Array(DIGEST_LENGTH);
|
|
101
|
+
out[0] = (state[0] >>> 24) & 0xff;
|
|
102
|
+
out[1] = (state[0] >>> 16) & 0xff;
|
|
103
|
+
out[2] = (state[0] >>> 8) & 0xff;
|
|
104
|
+
out[3] = state[0] & 0xff;
|
|
105
|
+
out[4] = (state[1] >>> 24) & 0xff;
|
|
106
|
+
out[5] = (state[1] >>> 16) & 0xff;
|
|
107
|
+
out[6] = (state[1] >>> 8) & 0xff;
|
|
108
|
+
out[7] = state[1] & 0xff;
|
|
109
|
+
out[8] = (state[2] >>> 24) & 0xff;
|
|
110
|
+
out[9] = (state[2] >>> 16) & 0xff;
|
|
111
|
+
out[10] = (state[2] >>> 8) & 0xff;
|
|
112
|
+
out[11] = state[2] & 0xff;
|
|
113
|
+
out[12] = (state[3] >>> 24) & 0xff;
|
|
114
|
+
out[13] = (state[3] >>> 16) & 0xff;
|
|
115
|
+
out[14] = (state[3] >>> 8) & 0xff;
|
|
116
|
+
out[15] = state[3] & 0xff;
|
|
117
|
+
out[16] = (state[4] >>> 24) & 0xff;
|
|
118
|
+
out[17] = (state[4] >>> 16) & 0xff;
|
|
119
|
+
out[18] = (state[4] >>> 8) & 0xff;
|
|
120
|
+
out[19] = state[4] & 0xff;
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
static normalizeKey(secret) {
|
|
124
|
+
const key = toUint8Array(secret);
|
|
125
|
+
if (key.byteLength > BLOCK) {
|
|
126
|
+
const h = new Sha1Js();
|
|
127
|
+
h.update(key);
|
|
128
|
+
const digest = h.digestSync();
|
|
129
|
+
const padded = new Uint8Array(BLOCK);
|
|
130
|
+
padded.set(digest);
|
|
131
|
+
return padded;
|
|
132
|
+
}
|
|
133
|
+
const padded = new Uint8Array(BLOCK);
|
|
134
|
+
padded.set(key);
|
|
135
|
+
return padded;
|
|
136
|
+
}
|
|
137
|
+
hashBuffer(data, offset) {
|
|
138
|
+
this.hashBufferWith(this.state, data, offset);
|
|
139
|
+
}
|
|
140
|
+
hashBufferWith(state, data, offset) {
|
|
141
|
+
const w = (this.w ??= new Int32Array(80));
|
|
142
|
+
let s0 = state[0], s1 = state[1], s2 = state[2], s3 = state[3], s4 = state[4];
|
|
143
|
+
for (let t = 0; t < 16; ++t) {
|
|
144
|
+
w[t] =
|
|
145
|
+
((data[offset + t * 4] & 0xff) << 24) |
|
|
146
|
+
((data[offset + t * 4 + 1] & 0xff) << 16) |
|
|
147
|
+
((data[offset + t * 4 + 2] & 0xff) << 8) |
|
|
148
|
+
(data[offset + t * 4 + 3] & 0xff);
|
|
149
|
+
}
|
|
150
|
+
for (let t = 16; t < 80; ++t) {
|
|
151
|
+
const x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
|
|
152
|
+
w[t] = (x << 1) | (x >>> 31);
|
|
153
|
+
}
|
|
154
|
+
for (let t = 0; t < 80; ++t) {
|
|
155
|
+
const r = t < 20 ? 0 : t < 40 ? 1 : t < 60 ? 2 : 3;
|
|
156
|
+
const temp = (((((s0 << 5) | (s0 >>> 27)) +
|
|
157
|
+
(r === 0 ? (s1 & s2) ^ (~s1 & s3) : r === 2 ? (s1 & s2) ^ (s1 & s3) ^ (s2 & s3) : s1 ^ s2 ^ s3)) |
|
|
158
|
+
0) +
|
|
159
|
+
((s4 + ((K[r] + w[t]) | 0)) | 0)) |
|
|
160
|
+
0;
|
|
161
|
+
s4 = s3;
|
|
162
|
+
s3 = s2;
|
|
163
|
+
s2 = (s1 << 30) | (s1 >>> 2);
|
|
164
|
+
s1 = s0;
|
|
165
|
+
s0 = temp;
|
|
166
|
+
}
|
|
167
|
+
state[0] = (state[0] + s0) | 0;
|
|
168
|
+
state[1] = (state[1] + s1) | 0;
|
|
169
|
+
state[2] = (state[2] + s2) | 0;
|
|
170
|
+
state[3] = (state[3] + s3) | 0;
|
|
171
|
+
state[4] = (state[4] + s4) | 0;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createHash, createHmac } from "node:crypto";
|
|
2
|
+
import { Sha1Js } from "./Sha1Js";
|
|
3
|
+
const hasNativeCrypto = (() => {
|
|
4
|
+
try {
|
|
5
|
+
createHash("sha1");
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
})();
|
|
12
|
+
export const Sha1Node = hasNativeCrypto ? buildNativeClass() : Sha1Js;
|
|
13
|
+
function buildNativeClass() {
|
|
14
|
+
return class Sha1Node {
|
|
15
|
+
digestLength = 20;
|
|
16
|
+
secret;
|
|
17
|
+
hash;
|
|
18
|
+
isHmac;
|
|
19
|
+
finished = false;
|
|
20
|
+
constructor(secret) {
|
|
21
|
+
this.secret = secret;
|
|
22
|
+
this.isHmac = !!secret;
|
|
23
|
+
this.hash = this.createHash();
|
|
24
|
+
}
|
|
25
|
+
update(data) {
|
|
26
|
+
if (this.finished) {
|
|
27
|
+
throw new Error("Attempted to update an already finished hash.");
|
|
28
|
+
}
|
|
29
|
+
this.hash.update(data);
|
|
30
|
+
}
|
|
31
|
+
async digest() {
|
|
32
|
+
let buf;
|
|
33
|
+
if (this.isHmac) {
|
|
34
|
+
this.finished = true;
|
|
35
|
+
buf = this.hash.digest();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
buf = this.hash.copy().digest();
|
|
39
|
+
}
|
|
40
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
41
|
+
}
|
|
42
|
+
reset() {
|
|
43
|
+
this.hash = this.createHash();
|
|
44
|
+
this.finished = false;
|
|
45
|
+
}
|
|
46
|
+
createHash() {
|
|
47
|
+
return this.secret ? createHmac("sha1", toBuffer(this.secret)) : createHash("sha1");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function toBuffer(data) {
|
|
52
|
+
if (typeof data === "string") {
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
if (ArrayBuffer.isView(data)) {
|
|
56
|
+
return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
57
|
+
}
|
|
58
|
+
return Buffer.from(data);
|
|
59
|
+
}
|