A verification mechanism using multiple different keys stored in a keychain to
cryptographically sign data using SHA256 or SHA512 with HMAC. Code based on
https://github.com/crypto-utils/keygrip
.
import { Algorithm, Keygrip } from "https://deno.land/x/keygrip/mod.ts";
Creates a class for storing private keys in a keychain to easily hash and verify data with multiple different keys.
Returns: A new instance of the keygrip class
keys {string[]}
-algo {Algorithm}
-
Cryptographically sign data by a secret key
Returns: a string with the signed hash of the data.
data {string}
- Data to signkey {string|number}
- If key is a number it resolves the key as an index in the keychain. If it is a string it is used directly. Defaults to first key in the keychain.
Verifies if the data matches the digest with any of the keys in the keychain.
Returns: A boolean if any key in the chain could be used to achieve the same digest.
data {string}
- Data to digest and verifydigest {string}
- Digested hash to compare against
Resolve the index of which key was used to digest specified data.
Returns: A number from -1 to the last index of the keychain
data {string}
- Data to digestdigest {string}
- Digested hash to compare against
import { Algorithm, Keygrip } from "https://deno.land/x/keygrip/mod.ts";
// Uses sha256 by default, could be changed to sha512 with second parameter
const keygrip = new Keygrip(["shh", "secret", "keys"]);
// Sign data with default key
const hash = keygrip.sign("some_important_data");
// Verify the data using the hash
keygrip.verify("some_important_data", hash);
// returns true
// Get index of key in keygrip
keygrip.index("some_important_data", hash);
// returns 0, for key at index 0 in the Keygrip keychain
$ deno test