Skip to content

Commit ebbfcff

Browse files
authored
[appconf] use SHA-256 helpers from core-util instead (Azure#21715)
and remove its own helpers.
1 parent a28ea00 commit ebbfcff

File tree

6 files changed

+24
-74
lines changed

6 files changed

+24
-74
lines changed

sdk/appconfiguration/app-configuration/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- Notable changes include Removal of `@opentelemetry/api` as a transitive dependency and ensuring that the active context is properly propagated.
2121
- Customers who would like to continue using OpenTelemetry driven tracing should visit our [OpenTelemetry Instrumentation](https://www.npmjs.com/package/@azure/opentelemetry-instrumentation-azure-sdk) package for instructions.
2222

23+
- Move to depend on `@azure/core-util` for SHA256 Digest and HMAC computing.
24+
2325
## 1.3.1 (2021-12-14)
2426

2527
### Bugs Fixed

sdk/appconfiguration/app-configuration/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
"bugs": {
2222
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
2323
},
24-
"browser": {
25-
"./dist-esm/src/internal/cryptoHelpers.js": "./dist-esm/src/internal/cryptoHelpers.browser.js"
26-
},
2724
"react-native": {
2825
"./dist/index.js": "./dist-esm/src/index.js"
2926
},
@@ -94,6 +91,7 @@
9491
"@azure/core-rest-pipeline": "^1.6.0",
9592
"@azure/core-tracing": "^1.0.0",
9693
"@azure/core-auth": "^1.3.0",
94+
"@azure/core-util": "^1.0.0",
9795
"tslib": "^2.2.0"
9896
},
9997
"devDependencies": {

sdk/appconfiguration/app-configuration/src/appConfigCredential.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
PipelineResponse,
88
SendRequest,
99
} from "@azure/core-rest-pipeline";
10-
import { sha256Digest, sha256Hmac } from "./internal/cryptoHelpers";
10+
import { computeSha256Hash, computeSha256Hmac } from "@azure/core-util";
1111

1212
/**
1313
* Create an HTTP pipeline policy to authenticate a request
@@ -19,13 +19,13 @@ export function appConfigKeyCredentialPolicy(credential: string, secret: string)
1919
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
2020
const verb = request.method;
2121
const utcNow = new Date().toUTCString();
22-
const contentHash = await sha256Digest(request.body?.toString() || "");
22+
const contentHash = await computeSha256Hash(request.body?.toString() || "", "base64");
2323
const signedHeaders = "x-ms-date;host;x-ms-content-sha256";
2424
const url = new URL(request.url);
2525
const query = url.search;
2626
const urlPathAndQuery = query ? `${url.pathname}${query}` : url.pathname;
2727
const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${url.host};${contentHash}`;
28-
const signature = await sha256Hmac(secret, stringToSign);
28+
const signature = await computeSha256Hmac(secret, stringToSign, "base64");
2929

3030
request.headers.set("x-ms-date", utcNow);
3131
request.headers.set("x-ms-content-sha256", contentHash);

sdk/appconfiguration/app-configuration/src/internal/cryptoHelpers.browser.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

sdk/appconfiguration/app-configuration/src/internal/cryptoHelpers.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

sdk/core/core-util/src/sha256.browser.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ function getCrypto(): SubtleCrypto {
6767
return subtleCrypto;
6868
}
6969

70-
const importParams: HmacImportParams = {
71-
name: "HMAC",
72-
hash: { name: "SHA-256" },
73-
};
74-
7570
/**
7671
* Generates a SHA-256 HMAC signature.
7772
* @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
@@ -87,8 +82,24 @@ export async function computeSha256Hmac(
8782
const keyBytes = base64ToBytes(key);
8883
const stringToSignBytes = utf8ToBytes(stringToSign);
8984

90-
const cryptoKey = await crypto.importKey("raw", keyBytes, importParams, false, ["sign"]);
91-
const signature = await crypto.sign(importParams, cryptoKey, stringToSignBytes);
85+
const cryptoKey = await crypto.importKey(
86+
"raw",
87+
keyBytes,
88+
{
89+
name: "HMAC",
90+
hash: { name: "SHA-256" },
91+
},
92+
false,
93+
["sign"]
94+
);
95+
const signature = await crypto.sign(
96+
{
97+
name: "HMAC",
98+
hash: { name: "SHA-256" },
99+
},
100+
cryptoKey,
101+
stringToSignBytes
102+
);
92103

93104
switch (encoding) {
94105
case "base64":

0 commit comments

Comments
 (0)