diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a6d5b64 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# v0.1.0 / 2014-01-04 + +Initial release diff --git a/README.md b/README.md index bdd4853..75c737c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,18 @@ # js-sha1 This is a simple SHA1 hash function for JavaScript supports UTF-8 encoding. +## Install +For node.js, you can use this command to install: + + npm install js-sha1 + ## Usage +If you use node.js, you should require the module first: + + var sha1 = require('js-sha1'); + +And you could use like this: + sha1('Message to hash'); ## Example @@ -25,9 +36,11 @@ Output 7be2d2d20c106eee0836c9bc2b939890a78e8fb3 -## Run Tests +## Tests You can open `tests/index.html` in browser or use node.js to run `node tests/node-test.js` for test. +You also could use `npm test` instance of `node tests/node-test.js`. + ## Extensions ### jQuery If you prefer jQuery style, you can add following code to add a jQuery extension. @@ -49,3 +62,7 @@ Code And then you could use like this: 'message'.sha1(); + +## Contact +The project's website is located at https://github.com/emn178/js-sha1 +Author: emn178@gmail.com diff --git a/build/sha1.min.js b/build/sha1.min.js index 41a5dd4..510a161 100644 --- a/build/sha1.min.js +++ b/build/sha1.min.js @@ -1,3 +1,3 @@ -(function(w,x){var t="0123456789abcdef".split(""),v={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15};w.sha1=function(e){var a;a:{for(a=e.length;a--;)if(255>2]=37==r?e[a>>2]|(v[c.charAt(++d)]<<4|v[c.charAt(++d)])<<(3-a%4<<3):e[a>>2]|r<<(3-a%4<<3);++a}c=(a+8>>6)+1<<4;d=a>>2;e[d]|=128<<(3-a%4<<3);for(d+=1;d>2]=37==r?e[a>>2]|(w[c.charAt(++d)]<<4|w[c.charAt(++d)])<<(3-a%4<<3):e[a>>2]|r<<(3-a%4<<3);++a}c=(a+8>>6)+1<<4;d=a>>2;e[d]|=128<<(3-a%4<<3);for(d+=1;d>6)+1<<4;c=[];for(f=0;f>2]|=e.charCodeAt(f)<<(3-f%4<<3);c[f>>2]|=128<<(3-f%4<<3);c[d-1]=a<<3;e=c}a=1732584193;for(var d=4023233417,c=2562383102,f=271733878,r=3285377520,u=0,t=e.length;ub;++b)n[b]=e[u+b];for(b=16;80>b;++b)n[b]=p(n[b-3]^n[b-8]^n[b-14]^n[b-16],1);for(var m=a,h=d,k=c,l=f,q=r,g,b=0;20>b;++b)g=h&k|~h&l,g=p(m,5)+g+q+1518500249+n[b],q=l,l=k,k=p(h,30),h=m,m=g;for(;40>b;++b)g=h^k^l,g=p(m,5)+ -g+q+1859775393+n[b],q=l,l=k,k=p(h,30),h=m,m=g;for(;60>b;++b)g=h&k|h&l|k&l,g=p(m,5)+g+q+2400959708+n[b],q=l,l=k,k=p(h,30),h=m,m=g;for(;80>b;++b)g=h^k^l,g=p(m,5)+g+q+3395469782+n[b],q=l,l=k,k=p(h,30),h=m,m=g;a+=m;d+=h;c+=k;f+=l;r+=q}return s(a)+s(d)+s(c)+s(f)+s(r)};var p=function(e,a){return e<>>32-a},s=function(e){for(var a="",c=0;4>c;c++)var d=3-c<<3,a=a+(t[e>>d+4&15]+t[e>>d&15]);return a}})(window); +g+q+1859775393+n[b],q=l,l=k,k=p(h,30),h=m,m=g;for(;60>b;++b)g=h&k|h&l|k&l,g=p(m,5)+g+q+2400959708+n[b],q=l,l=k,k=p(h,30),h=m,m=g;for(;80>b;++b)g=h^k^l,g=p(m,5)+g+q+3395469782+n[b],q=l,l=k,k=p(h,30),h=m,m=g;a+=m;d+=h;c+=k;f+=l;r+=q}return s(a)+s(d)+s(c)+s(f)+s(r)},p=function(e,a){return e<>>32-a},s=function(e){for(var a="",c=0;4>c;c++)var d=3-c<<3,a=a+(v[e>>d+4&15]+v[e>>d&15]);return a};"undefined"!=typeof module?module.exports=x:t&&(t.sha1=x)})(this); diff --git a/package.json b/package.json new file mode 100644 index 0000000..c076bc7 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "js-sha1", + "version": "0.1.0", + "description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", + "main": "src/sha1.js", + "scripts": { + "test": "node tests/node-test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/emn178/js-sha1.git" + }, + "keywords": [ + "sha1", + "hash", + "encryption" + ], + "author": "emn178 ", + "homepage": "https://github.com/emn178/js-sha1", + "bugs": { + "url": "https://github.com/emn178/js-sha1/issues" + } +} diff --git a/src/sha1.js b/src/sha1.js index 019d477..da09f4f 100644 --- a/src/sha1.js +++ b/src/sha1.js @@ -1,4 +1,6 @@ -(function(window, undefined){ +(function(root, undefined){ + 'use strict'; + var HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; var HEX_TABLE = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, @@ -6,7 +8,7 @@ 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15 }; - window.sha1 = function(message) { + var sha1 = function(message) { var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); var h0 = 0x67452301; var h1 = 0xEFCDAB89; @@ -146,4 +148,9 @@ blocks[blockCount - 1] = bytes << 3; // bytes * 8 return blocks; }; -}(window)); + + if(typeof(module) != 'undefined') + module.exports = sha1; + else if(root) + root.sha1 = sha1; +}(this)); diff --git a/tests/debug.js b/tests/debug.js index a9460b7..ceceea8 100644 --- a/tests/debug.js +++ b/tests/debug.js @@ -1,35 +1,12 @@ -(function(window){ - //console.time implementation for IE - if(window.console && typeof(window.console.time) == "undefined") { - console.time = function(name, reset){ - if(!name) { return; } - var time = new Date().getTime(); - if(!console.timeCounters) { console.timeCounters = {} }; - var key = "KEY" + name.toString(); - if(!reset && console.timeCounters[key]) { return; } - console.timeCounters[key] = time; - }; - - console.timeEnd = function(name){ - var time = new Date().getTime(); - if(!console.timeCounters) { return; } - var key = "KEY" + name.toString(); - var timeCounter = console.timeCounters[key]; - if(timeCounter) { - var diff = time - timeCounter; - var label = name + ": " + diff + "ms"; - console.info(label); - delete console.timeCounters[key]; - } - return diff; - }; - } - +(function(root){ var assert = function (title, expect, actual) { if(expect == actual) console.log(title + ': true'); else console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual); }; - window.assert = assert; -})(window); + if(typeof(module) != 'undefined') + global.assert = assert; + else if(root) + root.assert = assert; +})(this); diff --git a/tests/node-test.js b/tests/node-test.js index b3fd5e4..a57177a 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -1,4 +1,3 @@ -window = global; -require('../src/sha1.js'); +global.sha1 = require('../src/sha1.js'); require('./debug.js'); require('./test.js');