diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fbd8961..0f04b2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v2 - uses: denolib/setup-deno@master with: - deno-version: 1.1.0 + deno-version: 1.7.2 - run: deno --version - run: deno fmt --check diff --git a/README.md b/README.md index 7a59df4..3337cbd 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,26 @@ -keygrip -========================== +# keygrip -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`. +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`. - -API --------------------------- +## API ```js -import { Keygrip, Algorithm } from 'https://deno.land/x/keygrip/mod.ts'; +import { Algorithm, Keygrip } from "https://deno.land/x/keygrip/mod.ts"; ``` ### [keygrip = new Keygrip( keys [, algo=Algorithm.SHA256] )](#keygrip) -Creates a class for storing private keys in a keychain to easily hash and verify data with multiple different keys. +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 #### Parameters -* `keys {string[]}` - -* `algo {Algorithm}` - +- `keys {string[]}` - +- `algo {Algorithm}` - ### [keygrip.sign( data [, key] )](#sign) @@ -29,20 +29,23 @@ Cryptographically sign data by a secret key **Returns:** a string with the signed hash of the data. #### Parameters -* `data {string}` - Data to sign -* `key {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. +- `data {string}` - Data to sign +- `key {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. ### [keygrip.verify( data, digest )](#verify) 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. +**Returns:** A boolean if any key in the chain could be used to achieve the same +digest. #### Parameters -* `data {string}` - Data to digest and verify -* `digest {string}` - Digested hash to compare against +- `data {string}` - Data to digest and verify +- `digest {string}` - Digested hash to compare against ### [keygrip.index( data, digest )](#index) @@ -51,39 +54,36 @@ Resolve the index of which key was used to digest specified data. **Returns:** A number from -1 to the last index of the keychain #### Parameters -* `data {string}` - Data to digest -* `digest {string}` - Digested hash to compare against +- `data {string}` - Data to digest +- `digest {string}` - Digested hash to compare against -Examples --------------------------- +## Examples ```js -import { Keygrip, Algorithm } from 'https://deno.land/x/keygrip/mod.ts'; +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' ]); +const keygrip = new Keygrip(["shh", "secret", "keys"]); // Sign data with default key -const hash = keygrip.sign('some_important_data'); +const hash = keygrip.sign("some_important_data"); // Verify the data using the hash -keygrip.verify('some_important_data', hash); +keygrip.verify("some_important_data", hash); // returns true // Get index of key in keygrip -keygrip.index('some_important_data', hash); +keygrip.index("some_important_data", hash); // returns 0, for key at index 0 in the Keygrip keychain ``` -Testing --------------------------- +## Testing ```sh $ deno test ``` -License --------------------------- +## License [MIT](./LICENSE) diff --git a/deps.ts b/deps.ts index d790a99..bf3a599 100644 --- a/deps.ts +++ b/deps.ts @@ -1,4 +1,4 @@ // First party libraries -export { HmacSha256 } from "https://deno.land/std@v0.57.0/hash/sha256.ts"; -export { HmacSha512 } from "https://deno.land/std@v0.57.0/hash/sha512.ts"; -export * as base64url from "https://deno.land/std@v0.57.0/encoding/base64url.ts"; +export { HmacSha256 } from "https://deno.land/std@0.86.0/hash/sha256.ts"; +export { HmacSha512 } from "https://deno.land/std@0.86.0/hash/sha512.ts"; +export * as base64url from "https://deno.land/std@0.86.0/encoding/base64url.ts"; diff --git a/mod.ts b/mod.ts index ce20f2f..61fd143 100644 --- a/mod.ts +++ b/mod.ts @@ -4,7 +4,7 @@ * Copyright(c) 2020 Christian Norrman * MIT Licensed */ -import { HmacSha256, HmacSha512, base64url } from "./deps.ts"; +import { base64url, HmacSha256, HmacSha512 } from "./deps.ts"; /** * Enum of supported algorithms for keygrip @@ -61,7 +61,7 @@ export class Keygrip { break; } - return base64url.encode(buf); + return base64url.encode(new Uint8Array(buf)); } /** diff --git a/test.ts b/test.ts index 87d7d5d..1f0ebdb 100644 --- a/test.ts +++ b/test.ts @@ -1,8 +1,8 @@ import { assert, assertEquals, -} from "https://deno.land/std@v0.57.0/testing/asserts.ts"; -import { Keygrip, Algorithm } from "./mod.ts"; +} from "https://deno.land/std@0.86.0/testing/asserts.ts"; +import { Algorithm, Keygrip } from "./mod.ts"; const { test } = Deno; const keygrip = new Keygrip(["secret", "key", "right?"]);