fetch: Support shake128 and shake256 mechanisms
This commit is contained in:
parent
e708ea5e9c
commit
e98c5ba843
|
@ -55,6 +55,8 @@ npm install @peculiar/webcrypto
|
||||||
| PBKDF2 | | | X | | | | X |
|
| PBKDF2 | | | X | | | | X |
|
||||||
| DES-CBC<sup>2</sup>| X | | X | | X | X | |
|
| DES-CBC<sup>2</sup>| X | | X | | X | X | |
|
||||||
| DES-EDE3-CBC<sup>2</sup>| X | | X | | X | X | |
|
| DES-EDE3-CBC<sup>2</sup>| X | | X | | X | X | |
|
||||||
|
| shake128<sup>2</sup>| | X | | | | | |
|
||||||
|
| shake256<sup>2</sup>| | X | | | | | |
|
||||||
|
|
||||||
<sup>1</sup> Mechanism supports extended list of named curves `P-256`, `P-384`, `P-521`, `K-256`,
|
<sup>1</sup> Mechanism supports extended list of named curves `P-256`, `P-384`, `P-521`, `K-256`,
|
||||||
`brainpoolP160r1`, `brainpoolP160t1`, `brainpoolP192r1`, `brainpoolP192t1`, `brainpoolP224r1`, `brainpoolP224t1`, `brainpoolP256r1`, `brainpoolP256t1`, `brainpoolP320r1`, `brainpoolP320t1`, `brainpoolP384r1`, `brainpoolP384t1`, `brainpoolP512r1`, and `brainpoolP512t1`
|
`brainpoolP160r1`, `brainpoolP160t1`, `brainpoolP192r1`, `brainpoolP192t1`, `brainpoolP224r1`, `brainpoolP224t1`, `brainpoolP256r1`, `brainpoolP256t1`, `brainpoolP320r1`, `brainpoolP320t1`, `brainpoolP384r1`, `brainpoolP384t1`, `brainpoolP512r1`, and `brainpoolP512t1`
|
||||||
|
|
|
@ -7,3 +7,4 @@ export * from "./sha";
|
||||||
export * from "./pbkdf";
|
export * from "./pbkdf";
|
||||||
export * from "./hmac";
|
export * from "./hmac";
|
||||||
export * from "./hkdf";
|
export * from "./hkdf";
|
||||||
|
export * from "./shake";
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
import crypto from "crypto";
|
||||||
|
|
||||||
|
export class ShakeCrypto {
|
||||||
|
|
||||||
|
public static digest(algorithm: Algorithm, data: ArrayBuffer) {
|
||||||
|
const hash = crypto.createHash(algorithm.name.toLowerCase())
|
||||||
|
.update(Buffer.from(data)).digest();
|
||||||
|
return new Uint8Array(hash).buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
export * from "./crypto";
|
||||||
|
export * from "./shake128";
|
||||||
|
export * from "./shake256";
|
|
@ -0,0 +1,12 @@
|
||||||
|
import * as core from "webcrypto-core";
|
||||||
|
import { ShakeCrypto } from "./crypto";
|
||||||
|
|
||||||
|
export class Shake128Provider extends core.ProviderCrypto {
|
||||||
|
public name = "shake128";
|
||||||
|
public usages = [];
|
||||||
|
|
||||||
|
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
|
||||||
|
return ShakeCrypto.digest(algorithm, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
import * as core from "webcrypto-core";
|
||||||
|
import { ShakeCrypto } from "./crypto";
|
||||||
|
|
||||||
|
export class Shake256Provider extends core.ProviderCrypto {
|
||||||
|
public name = "shake256";
|
||||||
|
public usages = [];
|
||||||
|
|
||||||
|
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
|
||||||
|
return ShakeCrypto.digest(algorithm, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ import {
|
||||||
Pbkdf2Provider,
|
Pbkdf2Provider,
|
||||||
RsaEsProvider, RsaOaepProvider, RsaPssProvider, RsaSsaProvider,
|
RsaEsProvider, RsaOaepProvider, RsaPssProvider, RsaSsaProvider,
|
||||||
Sha1Provider, Sha256Provider, Sha384Provider, Sha512Provider,
|
Sha1Provider, Sha256Provider, Sha384Provider, Sha512Provider,
|
||||||
|
Shake128Provider, Shake256Provider,
|
||||||
} from "./mechs";
|
} from "./mechs";
|
||||||
|
|
||||||
export class SubtleCrypto extends core.SubtleCrypto {
|
export class SubtleCrypto extends core.SubtleCrypto {
|
||||||
|
@ -63,6 +64,13 @@ export class SubtleCrypto extends core.SubtleCrypto {
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
const nodeMajorVersion = /^v(\d+)/.exec(process.version)?.[1];
|
const nodeMajorVersion = /^v(\d+)/.exec(process.version)?.[1];
|
||||||
|
if (nodeMajorVersion && parseInt(nodeMajorVersion, 10) >= 12) {
|
||||||
|
//#region SHAKE
|
||||||
|
this.providers.set(new Shake128Provider());
|
||||||
|
this.providers.set(new Shake256Provider());
|
||||||
|
//#endregion
|
||||||
|
}
|
||||||
|
|
||||||
if (nodeMajorVersion && parseInt(nodeMajorVersion, 10) >= 14) {
|
if (nodeMajorVersion && parseInt(nodeMajorVersion, 10) >= 14) {
|
||||||
//#region EdDSA
|
//#region EdDSA
|
||||||
this.providers.set(new EdDsaProvider());
|
this.providers.set(new EdDsaProvider());
|
||||||
|
|
|
@ -226,4 +226,22 @@ context("Crypto", () => {
|
||||||
assert.strictEqual(hmacKey.algorithm.name, "HMAC");
|
assert.strictEqual(hmacKey.algorithm.name, "HMAC");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context("shake digest", () => {
|
||||||
|
|
||||||
|
const data = Buffer.from("test data");
|
||||||
|
|
||||||
|
it("shake128", async () => {
|
||||||
|
const hash = await crypto.subtle.digest("shake128", data);
|
||||||
|
|
||||||
|
assert.strictEqual(Buffer.from(hash).toString("hex"), "ae3bdcf04986a8e7ddd99ac948254693");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shake256", async () => {
|
||||||
|
const hash = await crypto.subtle.digest("shake256", data);
|
||||||
|
|
||||||
|
assert.strictEqual(Buffer.from(hash).toString("hex"), "be15253026b9a85e01ae54b1939284e8e514fbdad2a3bd5c1c0f437e60548e26");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Reference in New Issue