Skip to content

Commit 5726481

Browse files
authored
refactor: remove std/encoding/hex dep (#17)
1 parent 8549602 commit 5726481

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

deps.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

mod.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { hex } from "./deps.ts";
21
import { toAmz, toDateStamp } from "./src/date.ts";
32
export { toAmz, toDateStamp };
43
import { getSignatureKey, signAwsV4 } from "./src/signing.ts";
4+
import { sha256Hex } from "./src/util.ts";
55

66
/**
77
* Generic AWS Signer interface
@@ -96,15 +96,13 @@ export class AWSSignerV4 implements Signer {
9696
const body = request.body
9797
? new Uint8Array(await request.arrayBuffer())
9898
: null;
99-
const payloadHash = await sha256(body ?? new Uint8Array(0));
99+
const payloadHash = await sha256Hex(body ?? new Uint8Array(0));
100100

101101
const { awsAccessKeyId, awsSecretKey } = this.credentials;
102102

103103
const canonicalRequest =
104104
`${request.method}\n${pathname}\n${canonicalQuerystring}\n${canonicalHeaders}\n${signedHeaders}\n${payloadHash}`;
105-
const canonicalRequestDigest = await sha256(
106-
new TextEncoder().encode(canonicalRequest),
107-
);
105+
const canonicalRequestDigest = await sha256Hex(canonicalRequest);
108106

109107
const algorithm = "AWS4-HMAC-SHA256";
110108
const credentialScope =
@@ -159,9 +157,3 @@ export class AWSSignerV4 implements Signer {
159157
return AWS_REGION;
160158
};
161159
}
162-
163-
async function sha256(data: Uint8Array): Promise<string> {
164-
const hash = await crypto.subtle.digest("SHA-256", data);
165-
const hexBytes = hex.encode(new Uint8Array(hash));
166-
return new TextDecoder().decode(hexBytes);
167-
}

src/signing.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hex } from "../deps.ts";
1+
import { hex, hmacSha256 } from "./util.ts";
22

33
const AWS4: Uint8Array = new TextEncoder().encode("AWS4");
44

@@ -12,8 +12,7 @@ export async function signAwsV4(
1212
msg: string,
1313
): Promise<string> {
1414
const hash = await hmacSha256(key, msg);
15-
const hexBytes = hex.encode(hash);
16-
return new TextDecoder().decode(hexBytes);
15+
return hex(hash);
1716
}
1817

1918
/**
@@ -46,28 +45,3 @@ export async function getSignatureKey(
4645

4746
return new Uint8Array(mac);
4847
}
49-
50-
async function hmacSha256(
51-
key: string | Uint8Array,
52-
msg: string,
53-
): Promise<Uint8Array> {
54-
if (typeof key === "string") {
55-
key = new TextEncoder().encode(key);
56-
}
57-
58-
const hash = await crypto.subtle.importKey(
59-
"raw",
60-
key,
61-
{ name: "HMAC", hash: { name: "SHA-256" } },
62-
false,
63-
["sign"],
64-
);
65-
66-
const mac = await crypto.subtle.sign(
67-
{ name: "HMAC", hash: { name: "SHA-256" } },
68-
hash,
69-
new TextEncoder().encode(msg),
70-
);
71-
72-
return new Uint8Array(mac);
73-
}

src/util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export function hex(data: Uint8Array): string {
2+
return [...data]
3+
.map((b) => b.toString(16).padStart(2, "0"))
4+
.join("");
5+
}
6+
7+
export async function sha256Hex(data: string | Uint8Array): Promise<string> {
8+
if (typeof data === "string") {
9+
data = new TextEncoder().encode(data);
10+
}
11+
const hash = await crypto.subtle.digest("SHA-256", data);
12+
return hex(new Uint8Array(hash));
13+
}
14+
15+
export async function hmacSha256(
16+
keyData: string | Uint8Array,
17+
data: string,
18+
): Promise<Uint8Array> {
19+
if (typeof keyData === "string") {
20+
keyData = new TextEncoder().encode(keyData);
21+
}
22+
23+
const key = await crypto.subtle.importKey(
24+
"raw",
25+
keyData,
26+
{ name: "HMAC", hash: "SHA-256" },
27+
false,
28+
["sign"],
29+
);
30+
31+
const mac = await crypto.subtle.sign(
32+
{ name: "HMAC", hash: "SHA-256" },
33+
key,
34+
new TextEncoder().encode(data),
35+
);
36+
37+
return new Uint8Array(mac);
38+
}

0 commit comments

Comments
 (0)