support node.js

pull/2/merge
Chen Yi-Cyuan 12 years ago
parent 9d80e2d7f0
commit bcc167c953

@ -0,0 +1,3 @@
# v0.1.0 / 2014-01-04
Initial release

@ -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

4
build/sha1.min.js vendored

@ -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<e.charCodeAt(a)){a=!0;break a}a=!1}if(a){var c=encodeURIComponent(e);e=[];var d=0;a=0;for(var f=c.length;d<f;++d){var r=c.charCodeAt(d);e[a>>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<c;++d)e[d]=
(function(t,y){var v="0123456789abcdef".split(""),w={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},x=function(e){var a;a:{for(a=e.length;a--;)if(255<e.charCodeAt(a)){a=!0;break a}a=!1}if(a){var c=encodeURIComponent(e);e=[];var d=0;a=0;for(var f=c.length;d<f;++d){var r=c.charCodeAt(d);e[a>>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<c;++d)e[d]=
0;e[c-1]=a<<3}else{a=e.length;d=(a+8>>6)+1<<4;c=[];for(f=0;f<d;++f)c[f]=0;for(f=0;f<a;++f)c[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;u<t;u+=16){for(var n=[],b=0;16>b;++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<<a|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<<a|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);

@ -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 <emn178@gmail.com>",
"homepage": "https://github.com/emn178/js-sha1",
"bugs": {
"url": "https://github.com/emn178/js-sha1/issues"
}
}

@ -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));

@ -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);

@ -1,4 +1,3 @@
window = global;
require('../src/sha1.js');
global.sha1 = require('../src/sha1.js');
require('./debug.js');
require('./test.js');

Loading…
Cancel
Save