* Add bower package.

* Fixed JSHint warnings.
* Add travis.
* Add coveralls.
pull/2/merge v0.1.3
emn178 11 years ago
parent 7c0300fae5
commit c8da6f6851

1
.gitignore vendored

@ -0,0 +1 @@
node_modules/

@ -0,0 +1,12 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
install:
- npm install mocha -g
- npm install coveralls -g
- npm install mocha-lcov-reporter -g
- npm install
script:
- npm run-script coveralls

@ -1,3 +1,10 @@
# v0.1.3 / 2015-01-07
* Add bower package.
* Fixed JSHint warnings.
* Add travis.
* Add coveralls.
# v0.1.2 / 2014-07-27 # v0.1.2 / 2014-07-27
Fixed accents bug Fixed accents bug

@ -1,4 +1,4 @@
Copyright 2014 emn178@gmail.com Copyright 2014-2015 emn178@gmail.com
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

@ -1,20 +1,49 @@
# js-sha1 # js-sha1
[![Build Status](https://api.travis-ci.org/emn178/js-sha1.png)](https://travis-ci.org/emn178/js-sha1)
[![Build Status](https://coveralls.io/repos/emn178/js-sha1/badge.png?branch=master)](https://coveralls.io/r/emn178/js-sha1?branch=master)
[![NPM](https://nodei.co/npm/js-sha1.png?stars&downloads)](https://nodei.co/npm/js-sha1/)
A simple SHA1 hash function for JavaScript supports UTF-8 encoding. A simple SHA1 hash function for JavaScript supports UTF-8 encoding.
## Install ## Demo
[SHA1 Online](http://emn178.github.io/online-tools/sha1.html)
## Download
[Compress](https://raw.github.com/emn178/js-sha1/master/build/sha1.min.js)
[Uncompress](https://raw.github.com/emn178/js-sha1/master/src/sha1.js)
## Installation
You can also install js-sha1 by using Bower.
bower install js-sha1
For node.js, you can use this command to install: For node.js, you can use this command to install:
npm install js-sha1 npm install js-sha1
## Usage ## Usage
If you use node.js, you should require the module first: You could use like this:
```JavaScript ```JavaScript
sha1 = require('js-sha1'); sha1('Message to hash');
``` ```
And you could use like this: If you use node.js, you should require the module first:
```JavaScript ```JavaScript
sha1('Message to hash'); sha1 = require('js-sha1');
``` ```
### Methods
#### sha1(str, asciiOnly)
Hash string to sha1, set asciiOnly to true for better performace if you ensure input is ascii.
##### *str: `String`*
String to hash.
##### *asciiOnly: `Boolean` (default: `false`)*
Specify the string encoding is ASCII.
## Example ## Example
Code Code
```JavaScript ```JavaScript
@ -38,15 +67,6 @@ Output
7be2d2d20c106eee0836c9bc2b939890a78e8fb3 7be2d2d20c106eee0836c9bc2b939890a78e8fb3
## Tests
You can open `tests/index.html` in browser or use node.js to run test
node tests/node-test.js
or
npm test
## Extensions ## Extensions
### jQuery ### jQuery
If you prefer jQuery style, you can add following code to add a jQuery extension. If you prefer jQuery style, you can add following code to add a jQuery extension.

@ -0,0 +1,9 @@
{
"name": "js-sha1",
"version": "0.1.3",
"main": ["build/sha1.min.js"],
"ignore": [
"samples",
"tests"
]
}

@ -1,10 +1,15 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.1.2", "version": "0.1.3",
"description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha1.js", "main": "src/sha1.js",
"devDependencies": {
"expect.js": "~0.3.1",
"jscoverage": "~0.5.9"
},
"scripts": { "scripts": {
"test": "node tests/node-test.js" "test": "mocha tests/node-test.js -r jscoverage",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

@ -1,38 +1,36 @@
/* /*
* js-sha1 v0.1.2 * js-sha1 v0.1.3
* https://github.com/emn178/js-sha1 * https://github.com/emn178/js-sha1
* *
* Copyright 2014, emn178@gmail.com * Copyright 2014-2015, emn178@gmail.com
* *
* Licensed under the MIT license: * Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT * http://www.opensource.org/licenses/MIT
*/ */
;(function(root, undefined){
(function(root, undefined){
'use strict'; 'use strict';
var HEX_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; var HEX_CHARS = '0123456789abcdef'.split('');
var HEX_TABLE = {
'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
};
var sha1 = function(message) { var sha1 = function(message, asciiOnly) {
var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); var blocks, h0, h1, h2, h3, h4;
var h0 = 0x67452301; if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
var h1 = 0xEFCDAB89; blocks = getBlocksFromUtf8(message);
var h2 = 0x98BADCFE; } else {
var h3 = 0x10325476; blocks = getBlocksFromAscii(message);
var h4 = 0xC3D2E1F0; }
h0 = 0x67452301;
for(var i = 0, length = blocks.length;i < length;i += 16) h1 = 0xEFCDAB89;
{ h2 = 0x98BADCFE;
var w = []; h3 = 0x10325476;
for(var j = 0;j < 16;++j) h4 = 0xC3D2E1F0;
for(var i = 0, length = blocks.length;i < length;i += 16) {
var w = [], j;
for(j = 0;j < 16;++j) {
w[j] = blocks[i + j]; w[j] = blocks[i + j];
for(var j = 16;j < 80;++j) }
{ for(j = 16;j < 80;++j) {
var x = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; var x = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
w[j] = leftrotate(x, 1); w[j] = leftrotate(x, 1);
} }
@ -42,10 +40,9 @@
var c = h2; var c = h2;
var d = h3; var d = h3;
var e = h4; var e = h4;
var f, k, tmp; var f, tmp;
for(var j = 0;j < 20;++j) for(j = 0;j < 20;++j) {
{
f = (b & c) | ((~b) & d); f = (b & c) | ((~b) & d);
tmp = leftrotate(a, 5) + f + e + 0x5A827999 + w[j]; tmp = leftrotate(a, 5) + f + e + 0x5A827999 + w[j];
e = d; e = d;
@ -55,8 +52,7 @@
a = tmp; a = tmp;
} }
for(;j < 40;++j) for(;j < 40;++j) {
{
f = b ^ c ^ d; f = b ^ c ^ d;
tmp = leftrotate(a, 5) + f + e + 0x6ED9EBA1 + w[j]; tmp = leftrotate(a, 5) + f + e + 0x6ED9EBA1 + w[j];
e = d; e = d;
@ -67,8 +63,7 @@
} }
// k = 0x8F1BBCDC; // k = 0x8F1BBCDC;
for(;j < 60;++j) for(;j < 60;++j) {
{
f = (b & c) | (b & d) | (c & d); f = (b & c) | (b & d) | (c & d);
tmp = leftrotate(a, 5) + f + e + 0x8F1BBCDC + w[j]; tmp = leftrotate(a, 5) + f + e + 0x8F1BBCDC + w[j];
e = d; e = d;
@ -78,8 +73,7 @@
a = tmp; a = tmp;
} }
for(;j < 80;++j) for(;j < 80;++j) {
{
f = b ^ c ^ d; f = b ^ c ^ d;
tmp = leftrotate(a, 5) + f + e + 0xCA62C1D6 + w[j]; tmp = leftrotate(a, 5) + f + e + 0xCA62C1D6 + w[j];
e = d; e = d;
@ -104,63 +98,76 @@
}; };
var toHexString = function(num) { var toHexString = function(num) {
var hex = ""; var hex = '';
for(var i = 0; i < 4; i++) for(var i = 0; i < 4; i++) {
{
var offset = 3 - i << 3; var offset = 3 - i << 3;
hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F]; hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F];
} }
return hex; return hex;
}; };
var hasUTF8 = function(message) { var getBytesFromUtf8 = function(str) {
var i = message.length; var bytes = [], index = 0;
while(i--) for (var i = 0;i < str.length; i++) {
if(message.charCodeAt(i) > 127) var c = str.charCodeAt(i);
return true; if (c < 0x80) {
return false; bytes[index++] = c;
} else if (c < 0x800) {
bytes[index++] = 0xc0 | (c >> 6);
bytes[index++] = 0x80 | (c & 0x3f);
} else if (c < 0xd800 || c >= 0xe000) {
bytes[index++] = 0xe0 | (c >> 12);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
bytes[index++] = 0x80 | (c & 0x3f);
} else {
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
bytes[index++] = 0xf0 | (c >> 18);
bytes[index++] = 0x80 | ((c >> 12) & 0x3f);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
bytes[index++] = 0x80 | (c & 0x3f);
}
}
return bytes;
}; };
var ASCIItoBlocks = function(message) { var getBlocksFromAscii = function(message) {
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes) // a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
var length = message.length; var length = message.length;
var chunkCount = ((length + 8) >> 6) + 1; var chunkCount = ((length + 8) >> 6) + 1;
var blockCount = chunkCount << 4; // chunkCount * 16 var blockCount = chunkCount << 4; // chunkCount * 16
var blocks = []; var blocks = [], i;
var i; for(i = 0;i < blockCount;++i) {
for(i = 0;i < blockCount;++i)
blocks[i] = 0; blocks[i] = 0;
for(i = 0;i < length;++i) }
blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i % 4) << 3); for(i = 0;i < length;++i) {
blocks[i >> 2] |= 0x80 << (3 - (i % 4) << 3); blocks[i >> 2] |= message.charCodeAt(i) << (3 - (i & 3) << 3);
}
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
blocks[blockCount - 1] = length << 3; // length * 8 blocks[blockCount - 1] = length << 3; // length * 8
return blocks; return blocks;
}; };
var UTF8toBlocks = function(message) { var getBlocksFromUtf8 = function(message) {
var uri = encodeURIComponent(message); var bytes = getBytesFromUtf8(message);
var blocks = []; var length = bytes.length;
for(var i = 0, bytes = 0, length = uri.length;i < length;++i) var chunkCount = ((length + 8) >> 6) + 1;
{
var c = uri.charCodeAt(i);
if(c == 37) // %
blocks[bytes >> 2] |= ((HEX_TABLE[uri.charAt(++i)] << 4) | HEX_TABLE[uri.charAt(++i)]) << (3 - (bytes % 4) << 3);
else
blocks[bytes >> 2] |= c << (3 - (bytes % 4) << 3);
++bytes;
}
var chunkCount = ((bytes + 8) >> 6) + 1;
var blockCount = chunkCount << 4; // chunkCount * 16 var blockCount = chunkCount << 4; // chunkCount * 16
var index = bytes >> 2; var blocks = [], i;
blocks[index] |= 0x80 << (3 - (bytes % 4) << 3); for(i = 0;i < blockCount;++i) {
for(var i = index + 1;i < blockCount;++i)
blocks[i] = 0; blocks[i] = 0;
blocks[blockCount - 1] = bytes << 3; // bytes * 8 }
for(i = 0;i < length;++i) {
blocks[i >> 2] |= bytes[i] << (3 - (i & 3) << 3);
}
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
blocks[blockCount - 1] = length << 3; // length * 8
return blocks; return blocks;
}; };
if(typeof(module) != 'undefined') if(typeof(module) != 'undefined') {
module.exports = sha1; module.exports = sha1;
else if(root) }
else if(root) {
root.sha1 = sha1; root.sha1 = sha1;
}
}(this)); }(this));

@ -1,12 +0,0 @@
(function(root){
var assert = function (title, expect, actual) {
if(expect == actual)
console.log(title + ': true');
else
console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual);
};
if(typeof(module) != 'undefined')
global.assert = assert;
else if(root)
root.assert = assert;
})(this);

@ -3,10 +3,20 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SHA1</title> <title>SHA1</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
<script src="../src/sha1.js"></script> <script src="../src/sha1.js"></script>
<script src="debug.js"></script>
<script src="test.js"></script>
</head> </head>
<body> <body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
</script>
<script src="test.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body> </body>
</html> </html>

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

@ -1,5 +1,30 @@
assert('sha1 1', 'da39a3ee5e6b4b0d3255bfef95601890afd80709', sha1('')); describe('ascii', function() {
assert('sha1 2', '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', sha1('The quick brown fox jumps over the lazy dog')); describe('less than 64 bytes', function() {
assert('sha1 3', '408d94384216f890ff7a0c3528e8bed1e0b01621', sha1('The quick brown fox jumps over the lazy dog.')); it('should be successful', function() {
assert('sha1 4', '7be2d2d20c106eee0836c9bc2b939890a78e8fb3', sha1('中文')); expect(sha1('')).to.be('da39a3ee5e6b4b0d3255bfef95601890afd80709');
assert('sha1 5', '9e4e5d978deced901d621475b03f1ded19e945bf', sha1('aécio')); expect(sha1('The quick brown fox jumps over the lazy dog')).to.be('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12');
expect(sha1('The quick brown fox jumps over the lazy dog.', true)).to.be('408d94384216f890ff7a0c3528e8bed1e0b01621');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha1('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('8690faab7755408a03875895176fac318f14a699');
});
});
});
describe('UTF8', function() {
describe('less than 64 bytes', function() {
it('should be successful', function() {
expect(sha1('中文')).to.be('7be2d2d20c106eee0836c9bc2b939890a78e8fb3');
expect(sha1('aécio')).to.be('9e4e5d978deced901d621475b03f1ded19e945bf');
});
});
describe('more than 64 bytes', function() {
it('should be successful', function() {
expect(sha1('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('3a15ad3ce9efdd4bf982eaaaecdeda36a887a3f9');
});
});
});

Loading…
Cancel
Save