- TypeScript definitions.
- clone method.

Changed
- throw error if update after finalize
pull/22/head v0.8.0
Yi-Cyuan Chen 7 years ago
parent 241405d4fd
commit b6c443143f

@ -1,5 +1,13 @@
# Change Log # Change Log
## v0.8.0 / 2018-08-05
### Added
- TypeScript definitions.
- clone method.
### Changed
- throw error if update after finalize
## v0.7.1 / 2017-12-21 ## v0.7.1 / 2017-12-21
### Fixed ### Fixed
- incorrect result when first bit is 1 of bytes. - incorrect result when first bit is 1 of bytes.

@ -1,4 +1,4 @@
Copyright 2014-2017 Chen, Yi-Cyuan Copyright 2014-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

@ -5,6 +5,9 @@
[![NPM](https://nodei.co/npm/js-sha512.png?stars&downloads)](https://nodei.co/npm/js-sha512/) [![NPM](https://nodei.co/npm/js-sha512.png?stars&downloads)](https://nodei.co/npm/js-sha512/)
A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding. A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.
## Notice
* v0.8.0+ will throw an error if try to update hash after finalize.
## Demo ## Demo
[SHA512 Online](http://emn178.github.io/online-tools/sha512.html) [SHA512 Online](http://emn178.github.io/online-tools/sha512.html)
[SHA384 Online](http://emn178.github.io/online-tools/sha384.html) [SHA384 Online](http://emn178.github.io/online-tools/sha384.html)
@ -32,13 +35,29 @@ sha384('Message to hash');
sha512_256('Message to hash'); sha512_256('Message to hash');
sha512_224('Message to hash'); sha512_224('Message to hash');
// Support ArrayBuffer output
var arrayBuffer = sha512.arrayBuffer('Message to hash');
// Support Array output
var bytes = sha512.digest('Message to hash');
var bytes = sha512.array('Message to hash');
// update hash
var hash = sha512.update('Message to hash');
hash.update('Message2 to hash');
hash.hex();
// or to use create
var hash = sha512.create(); var hash = sha512.create();
hash.update('Message to hash'); hash.update('Message to hash');
hash.hex(); hash.hex();
var hash2 = sha512.update('Message to hash'); // clone
hash2.update('Message2 to hash'); var hash = sha512.update('Message to hash');
hash2.array(); var hash2 = hash.clone();
hash2.hex();
hash.update('Message2 to hash');
hash.hex();
// HMAC // HMAC
sha512.hmac('key', 'Message to hash'); sha512.hmac('key', 'Message to hash');

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

File diff suppressed because one or more lines are too long

151
index.d.ts vendored

@ -0,0 +1,151 @@
type Message = string | number[] | ArrayBuffer | Uint8Array;
interface Hasher {
/**
* Update hash
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
/**
* Return hash in hex string.
*/
hex(): string;
/**
* Return hash in hex string.
*/
toString(): string;
/**
* Return hash in ArrayBuffer.
*/
arrayBuffer(): ArrayBuffer;
/**
* Return hash in integer array.
*/
digest(): number[];
/**
* Return hash in integer array.
*/
array(): number[];
}
interface Hmac {
/**
* Computes a Hash-based message authentication code (HMAC) using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: string, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: string): Hasher;
/**
* Create a hash object and hash message using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
update(secretKey: string, message: Message): Hasher;
/**
* Return hash in hex string.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
hex(secretKey: string, message: Message): string;
/**
* Return hash in ArrayBuffer.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
arrayBuffer(secretKey: string, message: Message): ArrayBuffer;
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
digest(secretKey: string, message: Message): number[];
/**
* Return hash in integer array.
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
array(secretKey: string, message: Message): number[];
}
interface Hash {
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
(message: Message): string;
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
hex(message: Message): string;
/**
* Hash and return ArrayBuffer.
*
* @param message The message you want to hash.
*/
arrayBuffer(message: Message): ArrayBuffer;
/**
* Hash and return integer array.
*
* @param message The message you want to hash.
*/
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(): Hasher;
/**
* Create a hash object and hash message.
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
/**
* HMAC interface
*/
hmac: Hmac;
}
export var sha512: Hash;
export var sha384: Hash;
export var sha512_256: Hash;
export var sha512_224: Hash;

230
package-lock.json generated

@ -1,31 +1,68 @@
{ {
"name": "js-sha512", "name": "js-sha512",
"version": "0.7.1", "version": "0.8.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"bindings": { "bindings": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz",
"integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==", "integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==",
"dev": true "dev": true
}, },
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": {
"balanced-match": "1.0.0",
"concat-map": "0.0.1"
}
},
"browser-stdout": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
"dev": true
},
"commander": { "commander": {
"version": "2.3.0", "version": "2.15.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
"integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=", "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
"dev": true "dev": true
}, },
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"dev": true,
"requires": {
"ms": "2.0.0"
}
},
"diff": { "diff": {
"version": "1.4.0", "version": "3.5.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz", "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz",
"integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=", "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
"dev": true "dev": true
}, },
"escape-string-regexp": { "escape-string-regexp": {
"version": "1.0.2", "version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
"dev": true "dev": true
}, },
"expect.js": { "expect.js": {
@ -34,73 +71,67 @@
"integrity": "sha1-sKWaDS7/VDdUTr8M6qYBWEHQm1s=", "integrity": "sha1-sKWaDS7/VDdUTr8M6qYBWEHQm1s=",
"dev": true "dev": true
}, },
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true
},
"glob": { "glob": {
"version": "3.2.3", "version": "7.1.2",
"resolved": "https://registry.npmjs.org/glob/-/glob-3.2.3.tgz", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
"integrity": "sha1-4xPusknHr/qlxHUoaw4RW1mDlGc=", "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"graceful-fs": "2.0.3", "fs.realpath": "1.0.0",
"inflight": "1.0.6",
"inherits": "2.0.3", "inherits": "2.0.3",
"minimatch": "0.2.14" "minimatch": "3.0.4",
"once": "1.4.0",
"path-is-absolute": "1.0.1"
} }
}, },
"graceful-fs": { "growl": {
"version": "2.0.3", "version": "1.10.5",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-2.0.3.tgz", "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz",
"integrity": "sha1-fNLNsiiko/Nule+mzBQt59GhNtA=", "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==",
"dev": true "dev": true
}, },
"growl": { "has-flag": {
"version": "1.8.1", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/growl/-/growl-1.8.1.tgz", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
"integrity": "sha1-Sy3sjZB+k9szZiTc7AGDUC+MlCg=", "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
"dev": true "dev": true
}, },
"inherits": { "he": {
"version": "2.0.3", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
"dev": true "dev": true
}, },
"jade": { "inflight": {
"version": "0.26.3", "version": "1.0.6",
"resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true, "dev": true,
"requires": { "requires": {
"commander": "0.6.1", "once": "1.4.0",
"mkdirp": "0.3.0" "wrappy": "1.0.2"
},
"dependencies": {
"commander": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz",
"integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=",
"dev": true
},
"mkdirp": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz",
"integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=",
"dev": true
}
} }
}, },
"lru-cache": { "inherits": {
"version": "2.7.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
"dev": true "dev": true
}, },
"minimatch": { "minimatch": {
"version": "0.2.14", "version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha1-x054BXT2PG+aCQ6Q775u9TpqdWo=", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true, "dev": true,
"requires": { "requires": {
"lru-cache": "2.7.3", "brace-expansion": "1.1.11"
"sigmund": "1.0.1"
} }
}, },
"minimist": { "minimist": {
@ -110,47 +141,38 @@
"dev": true "dev": true
}, },
"mkdirp": { "mkdirp": {
"version": "0.5.0", "version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.0.tgz", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-HXMHam35hs2TROFecfzAWkyavxI=", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true, "dev": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
}, },
"mocha": { "mocha": {
"version": "2.3.4", "version": "5.2.0",
"resolved": "https://registry.npmjs.org/mocha/-/mocha-2.3.4.tgz", "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz",
"integrity": "sha1-himm+wRPLSJapLgaKuLQAWmesmY=", "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"commander": "2.3.0", "browser-stdout": "1.3.1",
"debug": "2.2.0", "commander": "2.15.1",
"diff": "1.4.0", "debug": "3.1.0",
"escape-string-regexp": "1.0.2", "diff": "3.5.0",
"glob": "3.2.3", "escape-string-regexp": "1.0.5",
"growl": "1.8.1", "glob": "7.1.2",
"jade": "0.26.3", "growl": "1.10.5",
"mkdirp": "0.5.0", "he": "1.1.1",
"supports-color": "1.2.0" "minimatch": "3.0.4",
}, "mkdirp": "0.5.1",
"dependencies": { "supports-color": "5.4.0"
"debug": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz",
"integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=",
"dev": true,
"requires": {
"ms": "0.7.1"
} }
}, },
"ms": { "ms": {
"version": "0.7.1", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
"dev": true "dev": true
}
}
}, },
"nan": { "nan": {
"version": "2.7.0", "version": "2.7.0",
@ -1752,18 +1774,27 @@
} }
} }
}, },
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": {
"wrappy": "1.0.2"
}
},
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
"dev": true
},
"requirejs": { "requirejs": {
"version": "2.3.5", "version": "2.3.5",
"resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.5.tgz", "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.5.tgz",
"integrity": "sha512-svnO+aNcR/an9Dpi44C7KSAy5fFGLtmPbaaCeQaklUz8BQhS64tWWIIlvEA5jrWICzlO/X9KSzSeXFnZdBu8nw==", "integrity": "sha512-svnO+aNcR/an9Dpi44C7KSAy5fFGLtmPbaaCeQaklUz8BQhS64tWWIIlvEA5jrWICzlO/X9KSzSeXFnZdBu8nw==",
"dev": true "dev": true
}, },
"sigmund": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz",
"integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=",
"dev": true
},
"source-map": { "source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@ -1771,10 +1802,13 @@
"dev": true "dev": true
}, },
"supports-color": { "supports-color": {
"version": "1.2.0", "version": "5.4.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
"integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4=", "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
"dev": true "dev": true,
"requires": {
"has-flag": "3.0.0"
}
}, },
"uglify-js": { "uglify-js": {
"version": "3.2.2", "version": "3.2.2",
@ -1803,6 +1837,12 @@
"bindings": "1.3.0", "bindings": "1.3.0",
"nan": "2.7.0" "nan": "2.7.0"
} }
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
} }
} }
} }

@ -1,19 +1,19 @@
{ {
"name": "js-sha512", "name": "js-sha512",
"version": "0.7.1", "version": "0.8.0",
"description": "This is a simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.", "description": "This is a simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.",
"main": "src/sha512.js", "main": "src/sha512.js",
"devDependencies": { "devDependencies": {
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"mocha": "~2.3.4", "mocha": "~5.2.0",
"nyc": "^11.3.0", "nyc": "^11.3.0",
"requirejs": "^2.1.22", "requirejs": "^2.1.22",
"uglify-js": "^3.1.9", "uglify-js": "^3.1.9",
"webworker-threads": "^0.7.11" "webworker-threads": "^0.7.11"
}, },
"scripts": { "scripts": {
"test": "nyc mocha tests/node-test.js", "test": "nyc mocha tests/node-test.js --exit",
"report": "nyc --reporter=html --reporter=text mocha tests/node-test.js", "report": "nyc --reporter=html --reporter=text mocha tests/node-test.js --exit",
"coveralls": "nyc report --reporter=text-lcov | coveralls", "coveralls": "nyc report --reporter=text-lcov | coveralls",
"build": "uglifyjs src/sha512.js -c -m eval --comments -o build/sha512.min.js" "build": "uglifyjs src/sha512.js -c -m eval --comments -o build/sha512.min.js"
}, },

@ -1,16 +1,17 @@
/* /*
* [js-sha512]{@link https://github.com/emn178/js-sha512} * [js-sha512]{@link https://github.com/emn178/js-sha512}
* *
* @version 0.7.1 * @version 0.8.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017 * @copyright Chen, Yi-Cyuan 2014-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_SHA512_NO_WINDOW) { if (root.JS_SHA512_NO_WINDOW) {
@ -222,22 +223,22 @@
Sha512.prototype.update = function (message) { Sha512.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;
} }
@ -796,21 +797,40 @@
return buffer; return buffer;
}; };
Sha512.prototype.clone = function () {
var hash = new Sha512(this.bits, false);
this.copyTo(hash);
return hash;
};
Sha512.prototype.copyTo = function (hash) {
var i = 0, attrs = [
'h0h', 'h0l', 'h1h', 'h1l', 'h2h', 'h2l', 'h3h', 'h3l', 'h4h', 'h4l', 'h5h', 'h5l', 'h6h', 'h6l', 'h7h', 'h7l',
'start', 'bytes', 'hBytes', 'finalized', 'hashed', 'lastByteIndex'
];
for (i = 0; i < attrs.length; ++i) {
hash[attrs[i]] = this[attrs[i]];
}
for (i = 0; i < this.blocks.length; ++i) {
hash.blocks[i] = this.blocks[i];
}
};
function HmacSha512(key, bits, sharedMemory) { function HmacSha512(key, bits, sharedMemory) {
var notString, type = typeof key; var notString, type = typeof key;
if (type !== 'string') { if (type !== 'string') {
if (type === 'object') { if (type === 'object') {
if (key === null) { if (key === null) {
throw ERROR; throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) { } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
key = new Uint8Array(key); key = new Uint8Array(key);
} else if (!Array.isArray(key)) { } else if (!Array.isArray(key)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
throw ERROR; throw new Error(INPUT_ERROR);
} }
} }
} else { } else {
throw ERROR; throw new Error(INPUT_ERROR);
} }
notString = true; notString = true;
} }
@ -871,6 +891,16 @@
} }
}; };
HmacSha512.prototype.clone = function () {
var hash = new HmacSha512([], this.bits, false);
this.copyTo(hash);
hash.inner = this.inner;
for (var i = 0; i < this.oKeyPad.length; ++i) {
hash.oKeyPad[i] = this.oKeyPad[i];
}
return hash;
};
var exports = createMethod(512); var exports = createMethod(512);
exports.sha512 = exports; exports.sha512 = exports;
exports.sha384 = createMethod(384); exports.sha384 = createMethod(384);

@ -203,9 +203,18 @@
call: function (key, message) { call: function (key, message) {
var hash = algorithm.update(key, message); var hash = algorithm.update(key, message);
hash.hex(); hash.hex();
hash.update(message); hash.finalize();
return hash.hex(); return hash.hex();
} }
},
{
name: 'clone',
call: function (key, message) {
var hash = algorithm.update(key, message);
var hash2 = hash.clone();
hash.update('any');
return hash2.hex();
}
} }
]; ];
@ -250,6 +259,16 @@
}); });
}); });
context('when update after finalize', function () {
it('should throw error', function () {
expect(function () {
var hash = algorithm.update('key', 'any');
hash.hex();
hash.update('any');
}).to.throwError(/finalize already called/);
});
});
describe('#' + name, function () { describe('#' + name, function () {
errorTestCases.forEach(function (testCase) { errorTestCases.forEach(function (testCase) {
context('when ' + testCase, function () { context('when ' + testCase, function () {

@ -260,9 +260,18 @@
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();
} }
},
{
name: 'clone',
call: function (message) {
var hash = algorithm.update(message);
var hash2 = hash.clone();
hash.update('any');
return hash2.hex();
}
} }
]; ];
@ -317,6 +326,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/);
});
});
context('when large size', function () { context('when large size', function () {
var hash = algorithm.create(); var hash = algorithm.create();
hash.bytes = 4294967295; hash.bytes = 4294967295;

Loading…
Cancel
Save