1 | import { Sha256 as Ie11Sha256 } from "./ie11Sha256";
|
2 | import { Sha256 as WebCryptoSha256 } from "./webCryptoSha256";
|
3 | import { Sha256 as JsSha256 } from "@aws-crypto/sha256-js";
|
4 | import { Hash, SourceData } from "@aws-sdk/types";
|
5 | import { supportsWebCrypto } from "@aws-crypto/supports-web-crypto";
|
6 | import { isMsWindow } from "@aws-crypto/ie11-detection";
|
7 | import { locateWindow } from "@aws-sdk/util-locate-window";
|
8 |
|
9 | export class Sha256 implements Hash {
|
10 | private readonly hash: Hash;
|
11 |
|
12 | constructor(secret?: SourceData) {
|
13 | if (supportsWebCrypto(locateWindow())) {
|
14 | this.hash = new WebCryptoSha256(secret);
|
15 | } else if (isMsWindow(locateWindow())) {
|
16 | this.hash = new Ie11Sha256(secret);
|
17 | } else {
|
18 | this.hash = new JsSha256(secret);
|
19 | }
|
20 | }
|
21 |
|
22 | update(data: SourceData, encoding?: "utf8" | "ascii" | "latin1"): void {
|
23 | this.hash.update(data, encoding);
|
24 | }
|
25 |
|
26 | digest(): Promise<Uint8Array> {
|
27 | return this.hash.digest();
|
28 | }
|
29 | }
|