- Added TypeScript definitions.

- Changed throw error if update after finalize
pull/23/head v0.8.0
Yi-Cyuan Chen 7 years ago
parent 1c00d97268
commit b39d0910cb

@ -1,5 +1,12 @@
# Change Log # Change Log
## v0.8.0 / 2018-08-05
### Added
- TypeScript definitions.
### Changed
- throw error if update after finalize
## v0.7.0 / 2017-12-01 ## v0.7.0 / 2017-12-01
### Added ### Added
- AMD support. - AMD support.

@ -1,4 +1,4 @@
Copyright 2015-2017 Chen, Yi-Cyuan Copyright 2015-2018 Chen, Yi-Cyuan
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

@ -7,6 +7,7 @@
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding. A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
## Notice ## Notice
* v0.8.0+ will throw an error if try to update hash after finalize.
* Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later. * Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
* `buffer` method is deprecated. This maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead. * `buffer` method is deprecated. This maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead.
@ -54,10 +55,11 @@ kmac128('key', 'Message to hash', 256, 'customization');
kmac256('key', 'Message to hash', 512, 'customization'); kmac256('key', 'Message to hash', 512, 'customization');
// Support ArrayBuffer output // Support ArrayBuffer output
var buffer = keccak224.buffer('Message to hash'); var arrayBuffer = keccak224.arrayBuffer('Message to hash');
// Support Array output // Support Array output
var buffer = keccak224.array('Message to hash'); var bytes = keccak224.digest('Message to hash');
var bytes = keccak224.array('Message to hash');
// update hash // update hash
sha3_512.update('Message ').update('to ').update('hash').hex(); sha3_512.update('Message ').update('to ').update('hash').hex();

@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.7.0", "version": "0.8.0",
"main": ["src/sha3.js"], "main": ["src/sha3.js"],
"ignore": [ "ignore": [
"samples", "samples",

6
build/sha3.min.js vendored

File diff suppressed because one or more lines are too long

139
index.d.ts vendored

@ -42,6 +42,13 @@ interface Hash {
*/ */
(message: Message): string; (message: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/** /**
* Hash and return ArrayBuffer. * Hash and return ArrayBuffer.
* *
@ -50,11 +57,18 @@ interface Hash {
arrayBuffer(message: Message): ArrayBuffer; arrayBuffer(message: Message): ArrayBuffer;
/** /**
* Hash and return ArrayBuffer. * Hash and return integer array.
* *
* @param message The message you want to hash. * @param message The message you want to hash.
*/ */
buffer(message: Message): ArrayBuffer; digest(message: Message): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
array(message: Message): number[];
/** /**
* Create a hash object. * Create a hash object.
@ -78,10 +92,43 @@ interface ShakeHash {
*/ */
(message: Message, outputBits: number): string; (message: Message, outputBits: number): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
hex(message: Message, outputBits: number): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
arrayBuffer(message: Message, outputBits: number): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
digest(message: Message, outputBits: number): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
array(message: Message, outputBits: number): number[];
/** /**
* Create a hash object. * Create a hash object.
* *
* @param outputBits The length of output. * @param outputBits The length of output.
* @param outputBits The length of output.
*/ */
create(outputBits: number): Hasher; create(outputBits: number): Hasher;
@ -105,6 +152,54 @@ interface CshakeHash {
*/ */
(message: Message, outputBits: number, functionName: Message, customization: Message): string; (message: Message, outputBits: number, functionName: Message, customization: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
hex(message: Message, outputBits: number, functionName: Message, customization: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
arrayBuffer(message: Message, outputBits: number, functionName: Message, customization: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
digest(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param functionName The function name string.
* @param customization The customization string.
*/
array(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
/**
* Create a hash object.
*
* @param outputBits The length of output.
* @param outputBits The length of output.
*/
create(outputBits: number): Hasher;
/** /**
* Create a hash object. * Create a hash object.
* *
@ -136,6 +231,46 @@ interface KmacHash {
*/ */
(key: Message, message: Message, outputBits: number, customization: Message): string; (key: Message, message: Message, outputBits: number, customization: Message): string;
/**
* Hash and return hex string.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
hex(key: Message, message: Message, outputBits: number, customization: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
arrayBuffer(key: Message, message: Message, outputBits: number, customization: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
digest(key: Message, message: Message, outputBits: number, customization: Message): number[];
/**
* Hash and return integer array.
*
* @param key The key string.
* @param message The message you want to hash.
* @param outputBits The length of output.
* @param customization The customization string.
*/
array(key: Message, message: Message, outputBits: number, customization: Message): number[];
/** /**
* Create a hash object. * Create a hash object.
* *

2
package-lock.json generated

@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.7.0", "version": "0.8.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.7.0", "version": "0.8.0",
"description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js", "main": "src/sha3.js",
"devDependencies": { "devDependencies": {

@ -1,16 +1,17 @@
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.7.0 * @version 0.8.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2017 * @copyright Chen, Yi-Cyuan 2015-2018
* @license MIT * @license MIT
*/ */
/*jslint bitwise: true */ /*jslint bitwise: true */
(function () { (function () {
'use strict'; 'use strict';
var ERROR = 'input is invalid type'; var INPUT_ERROR = 'input is invalid type';
var FINALIZE_ERROR = 'finalize already called';
var WINDOW = typeof window === 'object'; var WINDOW = typeof window === 'object';
var root = WINDOW ? window : {}; var root = WINDOW ? window : {};
if (root.JS_SHA3_NO_WINDOW) { if (root.JS_SHA3_NO_WINDOW) {
@ -185,22 +186,22 @@
Keccak.prototype.update = function (message) { Keccak.prototype.update = function (message) {
if (this.finalized) { if (this.finalized) {
return; throw new Error(FINALIZE_ERROR);
} }
var notString, type = typeof message; var notString, type = typeof message;
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (message === null) { if (message === null) {
throw ERROR; throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} else if (!Array.isArray(message)) { } else if (!Array.isArray(message)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
throw ERROR; throw new Error(INPUT_ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(INPUT_ERROR);
} }
notString = true; notString = true;
} }
@ -281,16 +282,16 @@
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (str === null) { if (str === null) {
throw ERROR; throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
str = new Uint8Array(str); str = new Uint8Array(str);
} else if (!Array.isArray(str)) { } else if (!Array.isArray(str)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
throw ERROR; throw new Error(INPUT_ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(INPUT_ERROR);
} }
notString = true; notString = true;
} }

@ -264,7 +264,7 @@
call: function (message) { call: function (message) {
var hash = algorithm.update(message); var hash = algorithm.update(message);
hash.hex(); hash.hex();
hash.update(message); hash.finalize();
return hash.hex(); return hash.hex();
} }
} }
@ -321,6 +321,16 @@
}); });
}); });
}); });
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = algorithm.update('any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
}); });
}); });
} }

Loading…
Cancel
Save