Spaces:
Configuration error
Configuration error
File size: 10,749 Bytes
5641073 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const wasm = __importStar(require("./node"));
const native = __importStar(require("./node-native"));
const chai_1 = require("chai");
const test_helpers_1 = require("./base/test-helpers");
const stream_buffers_1 = require("stream-buffers");
const hash_reader_1 = require("./base/hash-reader");
function suite({ hash, createHash, keyedHash, deriveKey, createDeriveKey, createKeyed, }) {
describe('encoding', () => {
it('hashes a buffer', () => {
chai_1.expect(hash(Buffer.from(test_helpers_1.inputs.hello.input))).to.deep.equal(test_helpers_1.inputs.hello.hash);
});
it('hashes a string', () => {
chai_1.expect(hash(test_helpers_1.inputs.hello.input)).to.deep.equal(test_helpers_1.inputs.hello.hash);
});
it('hashes an arraybuffer', () => {
const buf = Buffer.from(test_helpers_1.inputs.hello.input);
chai_1.expect(hash(new Uint8Array(buf).buffer)).to.deep.equal(test_helpers_1.inputs.hello.hash);
});
it('customizes the output length', () => {
chai_1.expect(hash(test_helpers_1.inputs.hello.input, { length: 16 })).to.deep.equal(test_helpers_1.inputs.hello.hash.slice(0, 16));
});
});
describe('memory-safety (#5)', () => {
it('hash', () => {
const hashA = hash('hello');
const hashB = hash('goodbye');
chai_1.expect(hashA.toString('hex')).to.equal('ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f');
chai_1.expect(hashB.toString('hex')).to.equal('f94a694227c5f31a07551908ad5fb252f5f0964030df5f2f200adedfae4d9b69');
});
it('hasher', () => {
const hasherA = createHash();
const hasherB = createHash();
hasherA.update('hel');
hasherB.update('good');
hasherA.update('lo');
hasherB.update('bye');
const hashA = hasherA.digest();
const hashB = hasherB.digest();
chai_1.expect(hashA.toString('hex')).to.equal('ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f');
chai_1.expect(hashB.toString('hex')).to.equal('f94a694227c5f31a07551908ad5fb252f5f0964030df5f2f200adedfae4d9b69');
});
});
describe('hasher', () => {
it('digests', callback => {
const buffer = new stream_buffers_1.ReadableStreamBuffer();
buffer.put(Buffer.from(test_helpers_1.inputs.large.input));
buffer.stop();
const hash = createHash();
buffer.on('data', b => hash.update(b));
buffer.on('end', () => {
const actual = hash.digest();
chai_1.expect(actual).to.deep.equal(test_helpers_1.inputs.large.hash);
callback();
});
});
it('is a transform stream', callback => {
const buffer = new stream_buffers_1.ReadableStreamBuffer();
buffer.put(Buffer.from(test_helpers_1.inputs.large.input));
buffer.stop();
buffer
.pipe(createHash())
.on('error', callback)
.on('data', hash => {
chai_1.expect(hash).to.deep.equal(test_helpers_1.inputs.large.hash);
callback();
});
});
it('customizes the output length', () => {
const hash = createHash();
hash.update(test_helpers_1.inputs.hello.input);
chai_1.expect(hash.digest('hex', { length: 16 })).to.equal(test_helpers_1.inputs.hello.hash.slice(0, 16).toString('hex'));
});
it('throws on write after dispose', () => {
const hash = createHash();
hash.dispose();
chai_1.expect(() => hash.update('')).to.throw(/after dispose/);
});
it('allows taking incremental hashes', () => {
const hasher = createHash();
hasher.update('hel');
const hashA = hasher.digest(undefined, { dispose: false });
const readA = hasher.reader({ dispose: false });
hasher.update('lo');
const hashB = hasher.digest(undefined, { dispose: false });
const readB = hasher.reader({ dispose: false });
const expectedA = Buffer.from('3121c5bb1b9193123447ac7cfda042f67f967e7a8cf5c12e7570e25529746e4a', 'hex');
chai_1.expect(hashA).to.deep.equal(expectedA);
chai_1.expect(readA.toBuffer()).to.deep.equal(expectedA);
chai_1.expect(hashB).to.deep.equal(test_helpers_1.inputs.hello.hash);
chai_1.expect(readB.toBuffer()).to.deep.equal(test_helpers_1.inputs.hello.hash);
hasher.dispose();
readA.dispose();
readB.dispose();
});
});
describe('reader', () => {
let reader;
beforeEach(() => {
const hash = createHash();
hash.update(test_helpers_1.inputs.hello.input);
reader = hash.reader();
});
afterEach(() => reader.dispose());
it('implements toString()', () => {
chai_1.expect(reader.toString('hex')).to.equal(test_helpers_1.inputs.hello.hash.toString('hex'));
reader.position = BigInt(42);
chai_1.expect(reader.toString('hex')).to.equal(test_helpers_1.inputs.hello.hash.toString('hex'));
});
it('implements toBuffer()', () => {
chai_1.expect(reader.toBuffer()).to.deep.equal(test_helpers_1.inputs.hello.hash);
reader.position = BigInt(42);
chai_1.expect(reader.toBuffer()).to.deep.equal(test_helpers_1.inputs.hello.hash);
});
it('implements readInto() and advances', () => {
const actual = Buffer.alloc(32);
reader.readInto(actual.slice(0, 10));
reader.readInto(actual.slice(10));
chai_1.expect(actual).to.deep.equal(test_helpers_1.inputs.hello.hash);
chai_1.expect(reader.position).to.equal(BigInt(32));
});
it('implements read() and advances', () => {
const actual = reader.read(32);
chai_1.expect(actual).to.deep.equal(test_helpers_1.inputs.hello.hash);
chai_1.expect(reader.position).to.equal(BigInt(32));
const actualNext = reader.read(16);
chai_1.expect(actualNext).to.deep.equal(test_helpers_1.hello48.slice(32));
chai_1.expect(reader.position).to.equal(BigInt(48));
});
it('manually sets position', () => {
reader.position = BigInt(32);
const actual = reader.read(16);
chai_1.expect(actual).to.deep.equal(test_helpers_1.hello48.slice(32));
});
it('throws if set out of range', () => {
chai_1.expect(() => (reader.position = BigInt(-1))).to.throw(RangeError);
chai_1.expect(() => (reader.position = BigInt('18446744073709551616'))).to.throw(RangeError);
reader.position = hash_reader_1.maxHashBytes - BigInt(1);
chai_1.expect(() => reader.read(2)).to.throw(RangeError);
});
});
describe('original test vectors', () => {
for (const { inputLen, expectedDerive, expectedKeyed, expectedHash } of test_helpers_1.ogTestVectors.cases) {
describe(`${inputLen}`, () => __awaiter(this, void 0, void 0, function* () {
const input = Buffer.alloc(inputLen);
for (let i = 0; i < inputLen; i++) {
input[i] = i % 251;
}
it('hash()', () => {
chai_1.expect(hash(input, { length: expectedHash.length / 2 }).toString('hex')).to.equal(expectedHash);
});
it('deriveKey()', () => {
chai_1.expect(deriveKey(test_helpers_1.ogTestVectors.context, input, { length: expectedDerive.length / 2 }).toString('hex')).to.equal(expectedDerive);
});
it('createDeriveKey()', callback => {
const buffer = new stream_buffers_1.ReadableStreamBuffer();
buffer.put(Buffer.from(input));
buffer.stop();
const hash = createDeriveKey(test_helpers_1.ogTestVectors.context);
buffer.on('data', b => hash.update(b));
buffer.on('end', () => {
const actual = hash.digest({ length: expectedDerive.length / 2 }).toString('hex');
chai_1.expect(actual).to.equal(expectedDerive);
callback();
});
});
it('keyedHash()', () => {
chai_1.expect(keyedHash(Buffer.from(test_helpers_1.ogTestVectors.key), input, {
length: expectedKeyed.length / 2,
}).toString('hex')).to.equal(expectedKeyed);
});
it('createKeyed()', callback => {
const buffer = new stream_buffers_1.ReadableStreamBuffer();
buffer.put(Buffer.from(input));
buffer.stop();
const hash = createKeyed(Buffer.from(test_helpers_1.ogTestVectors.key));
buffer.on('data', b => hash.update(b));
buffer.on('end', () => {
const actual = hash.digest({ length: expectedDerive.length / 2 }).toString('hex');
chai_1.expect(actual).to.equal(expectedKeyed);
callback();
});
});
}));
}
});
}
describe('node.js wasm', () => suite(wasm));
describe('node.js native', () => suite(native));
//# sourceMappingURL=node.test.js.map |