Skip to content

Commit

Permalink
amend definition file and check it against tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfictions committed Aug 3, 2018
1 parent d0103cb commit 8b207a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
21 changes: 14 additions & 7 deletions blake2s.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ export as namespace BLAKE2s;
export type ByteArray = number[] | Uint8Array;

export default class BLAKE2s {
constructor(digestLength: number | undefined, keyOrConfig: ByteArray | BLAKE2sConfig);
processBlock(length: number): void;
constructor(digestLength?: number);
constructor(digestLength: number | undefined, key: ByteArray);
constructor(digestLength: number | undefined, config: BLAKE2sConfig);
update(p: ByteArray, offset?: number, length?: number): this;
digest(): Uint8Array;
hexDigest(): string;
digestLength: number;
blockLength: number;
keyLength: number;
saltLength: number;
personalizationLength: number;
static readonly digestLength: 32;
static readonly blockLength: 64;
static readonly keyLength: 32;
static readonly saltLength: 8;
static readonly personalizationLength: 8;
}

export const digestLength: 32;
export const blockLength: 64;
export const keyLength: 32;
export const saltLength: 8;
export const personalizationLength: 8;

export interface BLAKE2sConfig {
key?: ByteArray;
personalization?: ByteArray;
Expand Down
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Run: node test.js

// @ts-check
/** @type {typeof import('../blake2s').default} */
// @ts-ignore
var BLAKE2s = typeof require !== 'undefined' ? require('../blake2s.js') : window.BLAKE2s;

var golden = [
Expand Down Expand Up @@ -643,6 +647,7 @@ var input = new Uint8Array([255, 254, 253, 252, 251, 250]);
console.log("Testing config validation");
var invalid = false;
try {
// @ts-ignore
var h = new BLAKE2s(32, { key: "not Uint8Array" });
} catch(e) {
invalid = true;
Expand Down Expand Up @@ -698,6 +703,7 @@ try {
}

try {
// @ts-ignore
h0 = new BLAKE2s(16, 'string');
console.log('did not throw for string key');
fails++;
Expand All @@ -706,7 +712,7 @@ try {
}

try {
h0 = new BLAKE2s(16, new Uint8Array(BLAKE2b.keyLength+1));
h0 = new BLAKE2s(16, new Uint8Array(BLAKE2s.keyLength+1));
console.log('did not throw for key.length = 33');
fails++;
} catch(e) {
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"allowJs": true,
},
"files": [
"blake2s.d.ts",
"test/test.js"
]
}

0 comments on commit 8b207a6

Please sign in to comment.