Skip to content

Commit d7966a3

Browse files
committed
use ByteUtils.encodeUTF8Into
1 parent 021f9de commit d7966a3

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/aws4.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,31 @@ export type SignedHeaders = {
2525
};
2626

2727
const getHash = async (str: string): Promise<string> => {
28-
const encoder = new TextEncoder();
29-
const data = encoder.encode(str);
28+
const data = new Uint8Array(BSON.onDemand.ByteUtils.utf8ByteLength(str));
29+
BSON.onDemand.ByteUtils.encodeUTF8Into(data, str, 0);
3030
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
3131
const hashHex = BSON.onDemand.ByteUtils.toHex(new Uint8Array(hashBuffer));
3232
return hashHex;
3333
};
3434
const getHmacBuffer = async (key: string | Uint8Array, str: string): Promise<Uint8Array> => {
35-
const encoder = new TextEncoder();
36-
const keyData = typeof key === 'string' ? encoder.encode(key) : key;
35+
let keyData: Uint8Array;
36+
if (typeof key === 'string') {
37+
keyData = new Uint8Array(BSON.onDemand.ByteUtils.utf8ByteLength(key));
38+
BSON.onDemand.ByteUtils.encodeUTF8Into(keyData, key, 0);
39+
} else {
40+
keyData = key;
41+
}
42+
3743
const importedKey = await crypto.subtle.importKey(
3844
'raw',
3945
keyData,
4046
{ name: 'HMAC', hash: { name: 'SHA-256' } },
4147
false,
4248
['sign']
4349
);
44-
const signature = await crypto.subtle.sign('HMAC', importedKey, encoder.encode(str));
50+
const strData = new Uint8Array(BSON.onDemand.ByteUtils.utf8ByteLength(str));
51+
BSON.onDemand.ByteUtils.encodeUTF8Into(strData, str, 0);
52+
const signature = await crypto.subtle.sign('HMAC', importedKey, strData);
4553
const digest = new Uint8Array(signature);
4654
return digest;
4755
};

0 commit comments

Comments
 (0)