This commit is contained in:
2022-07-21 03:28:35 +00:00
parent d7c883d6df
commit 51b34b0e1d
30103 changed files with 4152204 additions and 23 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules
.DS_Store
+104
View File
@@ -0,0 +1,104 @@
module.exports = function (grunt) {
'use strict';
var path = require('path');
var util = require('util');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.initConfig({
mochaTest: {
//node-side
any: {
src: ['test/setup.js', 'test/**/*.js'],
options: {
reporter: 'mocha-unfunk-reporter',
bail: false
}
}
},
uglify: {
main: {
options: {
report: 'min',
ASCIIOnly: true
},
files: {
'sha256.min.js': ['sha256.js']
}
}
}
});
grunt.registerTask('timing', function () {
[10, 1000, 100000, 10000000].forEach(function (length) {
var crypto = require('crypto');
var randomBytes = crypto.randomBytes(length);
var string = '';
for (var i = 0; i < randomBytes.length; i++) {
string += String.fromCharCode(randomBytes[i]);
}
var api = require('./');
var durationMs = 1000;
var start = Date.now(), iterations = 0;
while (Date.now() < start + durationMs) {
var hash = api(string);
iterations++;
}
var end = Date.now();
var averageMs = Math.round((end - start)/iterations*1000)/1000;
console.log(string.length + '-character string: ' + averageMs + 'ms');
});
});
grunt.registerTask('hack-uglify', function () {
var fs = require('fs');
var code = fs.readFileSync('sha256.min.js', {encoding: 'utf-8'});
code = code.replace(/\u0080/g, '\\x80');
fs.writeFileSync('sha256.min.js', code);
});
grunt.registerTask('build', function () {
var fs = require('fs'), path = require('path');
fs.readdirSync('templates').forEach(function (filename) {
if (filename.charAt(0) === '.') return;
var template = fs.readFileSync(path.join('templates', filename), {encoding: 'utf-8'});
var output = template.replace(/\{\{([^\:\{\}]+\:)?([^\{\}]+)\}\}/g, function (match, modifier, filename) {
var content = fs.readFileSync(filename, {encoding: 'utf-8'});
modifier = modifier && modifier.replace(':', '').toLowerCase();
if (modifier === 'html') {
content = content.replace(/</g, '&lt;').replace('"').replace(/"/, '&quot').replace(/'/g, '&#39');
} else if (modifier === 'json') {
content = JSON.stringify(content);
} else if (modifier === 'base64') {
content = (new Buffer(content, 'utf-8')).toString('base64');
}
return content;
});
fs.writeFileSync(filename, output);
console.log('Generated ' + filename);
});
});
grunt.registerTask('measure', function () {
var fs = require('fs');
var code = fs.readFileSync('sha256.min.js');
console.log('Minified length: ' + code.length + ' bytes');
// update byte count in package.json
var packageInfo = fs.readFileSync('package.json', {encoding: 'utf-8'});
packageInfo = packageInfo.replace(/("description":.*?)([0-9]+)( bytes)/, function (match, start, byteCount, end) {
return start + code.length + end;
});
fs.writeFileSync('package.json', packageInfo);
// update byte count in README
var readme = fs.readFileSync('README.md', {encoding: 'utf-8'});
readme = readme.replace(/(only )([0-9]+)( bytes)/, function (match, start, byteCount, end) {
return start + code.length + end;
});
fs.writeFileSync('README.md', readme);
});
grunt.registerTask('test', ['uglify', 'hack-uglify', 'build', 'mochaTest']);
grunt.registerTask('default', ['test', 'measure', 'timing']);
};
+29
View File
@@ -0,0 +1,29 @@
# A small SHA-256 implementation for JavaScript
The goals of this project are:
* small size - the minified version is only 849 bytes
* readability - the unminified code should be relatively easy to understand/review
Input must be an ASCII string - if character codes outside the range 0-255 are received, `undefined` is returned.
## In the browser
The code (`sha256.js` or `sha256.min.js`) defines the `sha256(string)` function, which returns the hexadecimal-encoded SHA-256 hash of the input string.
AMD is also supported - use `index.js` instead.
## In Node/CommonJS
If you're on Node, you should probably use the version from the built-in [`crypto` module](http://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm).
However, it is made available as a CommonJS module, including the source code for the minified version:
```javascript
var sha256 = require('tiny-sha256');
var jsCode = sha256.code + 'alert(sha256("hello!"));';
```
## License
This library is released as "public domain". You can copy, modify, re-release and re-license, or incorporate into any other project without restriction of any kind.
+170
View File
@@ -0,0 +1,170 @@
<html>
<head>
<title>JavaScript SHA256 demo</title>
<style>
body {
font-family: Arial, sans-serif;
font-size: 16px;
background-color: #EEE;
color: #222;
margin: 0;
padding: 0;
}
#content {
width: 900px;
margin: auto;
background-color: #E8E8E8;
padding: 1em;
}
h1 {
font-size: 1.4em;
text-align: center;
}
textarea {
width: 100%;
font-size: inherit;
border-radius: 3px;
padding: 0.3em;
}
#button {
width: 100%;
font-size: 0.8em;
line-height: 2em;
}
pre {
margin: 1em;
padding: 1em;
font-size: 12px;
background-color: #FFF;
border: 1px solid #BBB;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="content">
<h1>JavaScript SHA-256 demo</h1>
<p>This is a JavaScript implementation of SHA-256, aiming to be as small as I can make it. The goals are:</p>
<ul>
<li>small size- the minified version is <a href="sha256.min.js">less than a kilobyte</a></li>
<li>readability - the unminified version should be relatively easy to understand
</ul>
<p>It currently only supports ASCII, so if you need to hash Unicode text you'll need to write a decoder.</p>
<script src="sha256.min.js"></script>
<textarea id="input" rows=5>abc</textarea>
<input id="button" type="button" value="calculate" />
<textarea id="output" rows=1 style="text-align: center"></textarea>
<script>
document.getElementById('button').onclick = function () {
document.getElementById('output').value = sha256(document.getElementById('input').value);
};
</script>
<pre><code>var sha256 = function sha256(ascii) {
function rightRotate(value, amount) {
return (value>>>amount) | (value&lt;&lt;(32 - amount));
};
var mathPow = Math.pow;
var maxWord = mathPow(2, 32);
var lengthProperty = &#39length&#39;
var i, j; // Used as a counter across the whole file
var result = &#39&#39;
var words = [];
var asciiBitLength = ascii[lengthProperty]*8;
//* caching results is optional - remove/add slash from front of this line to toggle
// Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
// (we actually calculate the first 64, but extra values are just ignored)
var hash = sha256.h = sha256.h || [];
// Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
var k = sha256.k = sha256.k || [];
var primeCounter = k[lengthProperty];
/*/
var hash = [], k = [];
var primeCounter = 0;
//*/
var isComposite = {};
for (var candidate = 2; primeCounter &lt; 64; candidate++) {
if (!isComposite[candidate]) {
for (i = 0; i &lt; 313; i += candidate) {
isComposite[i] = candidate;
}
hash[primeCounter] = (mathPow(candidate, .5)*maxWord)|0;
k[primeCounter++] = (mathPow(candidate, 1/3)*maxWord)|0;
}
}
ascii += &#39\x80&#39; // Append &#391&#39 bit (plus zero padding)
while (ascii[lengthProperty]%64 - 56) ascii += &#39\x00&#39; // More zero padding
for (i = 0; i &lt; ascii[lengthProperty]; i++) {
j = ascii.charCodeAt(i);
if (j>>8) return; // ASCII check: only accept characters in range 0-255
words[i>>2] |= j &lt;&lt; ((3 - i)%4)*8;
}
words[words[lengthProperty]] = ((asciiBitLength/maxWord)|0);
words[words[lengthProperty]] = (asciiBitLength)
// process each chunk
for (j = 0; j &lt; words[lengthProperty];) {
var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
var oldHash = hash;
// This is now the undefinedworking hash&quot, often labelled as variables a...g
// (we have to truncate as well, otherwise extra entries at the end accumulate
hash = hash.slice(0, 8);
for (i = 0; i &lt; 64; i++) {
var i2 = i + j;
// Expand the message into 64 words
// Used below if
var w15 = w[i - 15], w2 = w[i - 2];
// Iterate
var a = hash[0], e = hash[4];
var temp1 = hash[7]
+ (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1
+ ((e&hash[5])^((~e)&hash[6])) // ch
+ k[i]
// Expand the message schedule if needed
+ (w[i] = (i &lt; 16) ? w[i] : (
w[i - 16]
+ (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15>>>3)) // s0
+ w[i - 7]
+ (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2>>>10)) // s1
)|0
);
// This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0
+ ((a&hash[1])^(a&hash[2])^(hash[1]&hash[2])); // maj
hash = [(temp1 + temp2)|0].concat(hash); // We don&#39t bother trimming off the extra ones, they&#39re harmless as long as we&#39re truncating when we do the slice()
hash[4] = (hash[4] + temp1)|0;
}
for (i = 0; i &lt; 8; i++) {
hash[i] = (hash[i] + oldHash[i])|0;
}
}
for (i = 0; i &lt; 8; i++) {
for (j = 3; j + 1; j--) {
var b = (hash[i]>>(j*8))&255;
result += ((b &lt; 16) ? 0 : &#39&#39) + b.toString(16);
}
}
return result;
};
</code></pre>
</div>
</body>
</html>
+113
View File
@@ -0,0 +1,113 @@
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module !== 'undefined' && module.exports){
module.exports = factory();
} else {
global.sha256 = factory();
}
})(this, function () {
var sha256 = function sha256(ascii) {
function rightRotate(value, amount) {
return (value>>>amount) | (value<<(32 - amount));
};
var mathPow = Math.pow;
var maxWord = mathPow(2, 32);
var lengthProperty = 'length';
var i, j; // Used as a counter across the whole file
var result = '';
var words = [];
var asciiBitLength = ascii[lengthProperty]*8;
//* caching results is optional - remove/add slash from front of this line to toggle
// Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
// (we actually calculate the first 64, but extra values are just ignored)
var hash = sha256.h = sha256.h || [];
// Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
var k = sha256.k = sha256.k || [];
var primeCounter = k[lengthProperty];
/*/
var hash = [], k = [];
var primeCounter = 0;
//*/
var isComposite = {};
for (var candidate = 2; primeCounter < 64; candidate++) {
if (!isComposite[candidate]) {
for (i = 0; i < 313; i += candidate) {
isComposite[i] = candidate;
}
hash[primeCounter] = (mathPow(candidate, .5)*maxWord)|0;
k[primeCounter++] = (mathPow(candidate, 1/3)*maxWord)|0;
}
}
ascii += '\x80'; // Append '1' bit (plus zero padding)
while (ascii[lengthProperty]%64 - 56) ascii += '\x00'; // More zero padding
for (i = 0; i < ascii[lengthProperty]; i++) {
j = ascii.charCodeAt(i);
if (j>>8) return; // ASCII check: only accept characters in range 0-255
words[i>>2] |= j << ((3 - i)%4)*8;
}
words[words[lengthProperty]] = ((asciiBitLength/maxWord)|0);
words[words[lengthProperty]] = (asciiBitLength)
// process each chunk
for (j = 0; j < words[lengthProperty];) {
var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
var oldHash = hash;
// This is now the "working hash", often labelled as variables a...g
// (we have to truncate as well, otherwise extra entries at the end accumulate
hash = hash.slice(0, 8);
for (i = 0; i < 64; i++) {
var i2 = i + j;
// Expand the message into 64 words
// Used below if
var w15 = w[i - 15], w2 = w[i - 2];
// Iterate
var a = hash[0], e = hash[4];
var temp1 = hash[7]
+ (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1
+ ((e&hash[5])^((~e)&hash[6])) // ch
+ k[i]
// Expand the message schedule if needed
+ (w[i] = (i < 16) ? w[i] : (
w[i - 16]
+ (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15>>>3)) // s0
+ w[i - 7]
+ (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2>>>10)) // s1
)|0
);
// This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0
+ ((a&hash[1])^(a&hash[2])^(hash[1]&hash[2])); // maj
hash = [(temp1 + temp2)|0].concat(hash); // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice()
hash[4] = (hash[4] + temp1)|0;
}
for (i = 0; i < 8; i++) {
hash[i] = (hash[i] + oldHash[i])|0;
}
}
for (i = 0; i < 8; i++) {
for (j = 3; j + 1; j--) {
var b = (hash[i]>>(j*8))&255;
result += ((b < 16) ? 0 : '') + b.toString(16);
}
}
return result;
};
sha256.code = "var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h=\"length\",i=\"\",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+=\"\\x80\";b[h]%64-56;)b+=\"\\x00\";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:\"\")+y.toString(16)}return i};";
return sha256;
});
+49
View File
@@ -0,0 +1,49 @@
{
"_args": [
[
"tiny-sha256@1.0.2",
"/home/node/nuxt"
]
],
"_from": "tiny-sha256@1.0.2",
"_id": "tiny-sha256@1.0.2",
"_inBundle": false,
"_integrity": "sha1-OyCnX3cJfc7Br1E/UYnCbsL1SZI=",
"_location": "/tiny-sha256",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "tiny-sha256@1.0.2",
"name": "tiny-sha256",
"escapedName": "tiny-sha256",
"rawSpec": "1.0.2",
"saveSpec": null,
"fetchSpec": "1.0.2"
},
"_requiredBy": [
"/@line/liff"
],
"_resolved": "https://registry.npmjs.org/tiny-sha256/-/tiny-sha256-1.0.2.tgz",
"_spec": "1.0.2",
"_where": "/home/node/nuxt",
"author": {
"name": "Geraint Luff"
},
"description": "SHA-256 in 849 bytes (minified)",
"devDependencies": {
"chai": "~1.9.1",
"grunt": "~0.4.5",
"grunt-contrib-uglify": "~0.5.1",
"grunt-mocha-test": "~0.12.0",
"mocha": "~1.21.4",
"mocha-unfunk-reporter": "~0.4.0"
},
"license": "Public domain",
"main": "index.js",
"name": "tiny-sha256",
"scripts": {
"test": "grunt test"
},
"version": "1.0.2"
}
+96
View File
@@ -0,0 +1,96 @@
var sha256 = function sha256(ascii) {
function rightRotate(value, amount) {
return (value>>>amount) | (value<<(32 - amount));
};
var mathPow = Math.pow;
var maxWord = mathPow(2, 32);
var lengthProperty = 'length';
var i, j; // Used as a counter across the whole file
var result = '';
var words = [];
var asciiBitLength = ascii[lengthProperty]*8;
//* caching results is optional - remove/add slash from front of this line to toggle
// Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
// (we actually calculate the first 64, but extra values are just ignored)
var hash = sha256.h = sha256.h || [];
// Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
var k = sha256.k = sha256.k || [];
var primeCounter = k[lengthProperty];
/*/
var hash = [], k = [];
var primeCounter = 0;
//*/
var isComposite = {};
for (var candidate = 2; primeCounter < 64; candidate++) {
if (!isComposite[candidate]) {
for (i = 0; i < 313; i += candidate) {
isComposite[i] = candidate;
}
hash[primeCounter] = (mathPow(candidate, .5)*maxWord)|0;
k[primeCounter++] = (mathPow(candidate, 1/3)*maxWord)|0;
}
}
ascii += '\x80'; // Append '1' bit (plus zero padding)
while (ascii[lengthProperty]%64 - 56) ascii += '\x00'; // More zero padding
for (i = 0; i < ascii[lengthProperty]; i++) {
j = ascii.charCodeAt(i);
if (j>>8) return; // ASCII check: only accept characters in range 0-255
words[i>>2] |= j << ((3 - i)%4)*8;
}
words[words[lengthProperty]] = ((asciiBitLength/maxWord)|0);
words[words[lengthProperty]] = (asciiBitLength)
// process each chunk
for (j = 0; j < words[lengthProperty];) {
var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
var oldHash = hash;
// This is now the "working hash", often labelled as variables a...g
// (we have to truncate as well, otherwise extra entries at the end accumulate
hash = hash.slice(0, 8);
for (i = 0; i < 64; i++) {
var i2 = i + j;
// Expand the message into 64 words
// Used below if
var w15 = w[i - 15], w2 = w[i - 2];
// Iterate
var a = hash[0], e = hash[4];
var temp1 = hash[7]
+ (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1
+ ((e&hash[5])^((~e)&hash[6])) // ch
+ k[i]
// Expand the message schedule if needed
+ (w[i] = (i < 16) ? w[i] : (
w[i - 16]
+ (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15>>>3)) // s0
+ w[i - 7]
+ (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2>>>10)) // s1
)|0
);
// This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0
+ ((a&hash[1])^(a&hash[2])^(hash[1]&hash[2])); // maj
hash = [(temp1 + temp2)|0].concat(hash); // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice()
hash[4] = (hash[4] + temp1)|0;
}
for (i = 0; i < 8; i++) {
hash[i] = (hash[i] + oldHash[i])|0;
}
}
for (i = 0; i < 8; i++) {
for (j = 3; j + 1; j--) {
var b = (hash[i]>>(j*8))&255;
result += ((b < 16) ? 0 : '') + b.toString(16);
}
}
return result;
};
+1
View File
@@ -0,0 +1 @@
var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i};
+74
View File
@@ -0,0 +1,74 @@
<html>
<head>
<title>JavaScript SHA256 demo</title>
<style>
body {
font-family: Arial, sans-serif;
font-size: 16px;
background-color: #EEE;
color: #222;
margin: 0;
padding: 0;
}
#content {
width: 900px;
margin: auto;
background-color: #E8E8E8;
padding: 1em;
}
h1 {
font-size: 1.4em;
text-align: center;
}
textarea {
width: 100%;
font-size: inherit;
border-radius: 3px;
padding: 0.3em;
}
#button {
width: 100%;
font-size: 0.8em;
line-height: 2em;
}
pre {
margin: 1em;
padding: 1em;
font-size: 12px;
background-color: #FFF;
border: 1px solid #BBB;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="content">
<h1>JavaScript SHA-256 demo</h1>
<p>This is a JavaScript implementation of SHA-256, aiming to be as small as I can make it. The goals are:</p>
<ul>
<li>small size- the minified version is <a href="sha256.min.js">less than a kilobyte</a></li>
<li>readability - the unminified version should be relatively easy to understand
</ul>
<p>It currently only supports ASCII, so if you need to hash Unicode text you'll need to write a decoder.</p>
<script src="sha256.min.js"></script>
<textarea id="input" rows=5>abc</textarea>
<input id="button" type="button" value="calculate" />
<textarea id="output" rows=1 style="text-align: center"></textarea>
<script>
document.getElementById('button').onclick = function () {
document.getElementById('output').value = sha256(document.getElementById('input').value);
};
</script>
<pre><code>{{html:sha256.js}}</code></pre>
</div>
</body>
</html>
+17
View File
@@ -0,0 +1,17 @@
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module !== 'undefined' && module.exports){
module.exports = factory();
} else {
global.sha256 = factory();
}
})(this, function () {
{{sha256.js}}
sha256.code = {{json:sha256.min.js}};
return sha256;
});
+29
View File
@@ -0,0 +1,29 @@
var fs = require('fs'), path = require('path');
var api = require('../');
var assert = require('chai').assert;
var minified = require('fs').readFileSync(path.join(__dirname, '../sha256.min.js'), {encoding: 'utf-8'});
var minifiedApi = (new Function (minified + 'return sha256;'))();
describe('Examples:', function () {
var examplesHex = {
"abc": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"test": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.": "2d8c2f6d978ca21712b5f6de36c9d31fa8e96a4fa5d8ff8b0188dfb9e7c171bb"
};
Object.keys(examplesHex).forEach(function (key) {
var input = key;
var expectedHex = examplesHex[key];
it(JSON.stringify(input.substring(0, 11)), function () {
var hex = api(input);
assert.equal(hex, expectedHex);
});
it('Minified: ' + JSON.stringify(input.substring(0, 11)), function () {
var hex = minifiedApi(input);
assert.equal(hex, expectedHex);
});
});
});