add strongestHashAlgorithms getter

This commit is contained in:
Nebel 2024-10-02 17:55:16 +09:00
parent c085fcbe8f
commit ab57344af3
Signed by: nebel
GPG key ID: 79807D08C6EF6460
2 changed files with 47 additions and 0 deletions

View file

@ -183,6 +183,14 @@ export class IntegrityMetadataSet {
return this.#set.length; return this.#set.length;
} }
get strongestHashAlgorithms(): ReadonlyArray<HashAlgorithm> {
const strongestHashAlgorithms = this.strongest
.map(({ alg }) => alg as HashAlgorithm)
.filter(Boolean);
return [...new Set(strongestHashAlgorithms)];
}
join(separator = " "): string { join(separator = " "): string {
return this.#set.map(String).join(separator); return this.#set.map(String).join(separator);
} }

View file

@ -0,0 +1,39 @@
import assert from "node:assert";
import { test } from "node:test";
import { IntegrityMetadataSet } from "../../dist/index.js";
test("pick the strongest hash algorithms", function () {
const integrityMetadataSet = new IntegrityMetadataSet(`
sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=
sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r
sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ==
`);
assert.deepEqual(integrityMetadataSet.strongestHashAlgorithms, ["sha512"]);
});
test("if there are no supported algorithms, return the empty set", function () {
const integrityMetadataSet = new IntegrityMetadataSet(`
sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk=
md5-bNNVbesNpUvKBgtMOUeYOQ==
`);
assert.deepEqual(integrityMetadataSet.strongestHashAlgorithms, []);
});
test("custom getPrioritizedHashAlgorithm function can be used", function () {
const integrityMetadataSet = new IntegrityMetadataSet(
`
sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=
sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r
sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ==
`,
{
getPrioritizedHashAlgorithm() {
return "sha384";
},
},
);
assert.deepEqual(integrityMetadataSet.strongestHashAlgorithms, ["sha384"]);
});