Skip to content

Commit

Permalink
Support byte array input
Browse files Browse the repository at this point in the history
  • Loading branch information
emn178 committed Feb 12, 2015
1 parent 1845772 commit f73267a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.2.3 / 2015-02-11

* Support byte array input.

# v0.2.2 / 2015-02-10

* Improve performance.
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ Output
72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21
dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4

It also supports byte Array or Uint8Array input:

Code
```JavaScript
sha256([]);
sha256(new Uint8Array([211, 212]));
```
Output

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f

## Benchmark
[UTF8](http://jsperf.com/sha256/66)
[ASCII](http://jsperf.com/sha256/65)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "0.2.2",
"version": "0.2.3",
"main": ["build/sha256.min.js"],
"ignore": [
"samples",
Expand Down
16 changes: 8 additions & 8 deletions build/sha256.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "0.2.2",
"version": "0.2.3",
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha256.js",
"devDependencies": {
Expand Down
48 changes: 30 additions & 18 deletions src/sha256.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* js-sha256 v0.2.2
* js-sha256 v0.2.3
* https://github.com/emn178/js-sha256
*
* Copyright 2014-2015, emn178@gmail.com
Expand All @@ -14,6 +14,7 @@
if(NODE_JS) {
root = global;
}
var TYPED_ARRAY = typeof(Uint8Array) != 'undefined';
var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [-2147483648, 8388608, 32768, 128];
var SHIFT = [24, 16, 8, 0];
Expand All @@ -28,6 +29,11 @@

var blocks = [];

Array.prototype.__ARRAY__ = true;
if(TYPED_ARRAY) {
Uint8Array.prototype.__ARRAY__ = true;
}

var sha224 = function(message) {
return sha256(message, true);
};
Expand Down Expand Up @@ -63,23 +69,29 @@
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
for (i = start;index < length && i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
if(message.__ARRAY__) {
for (i = start;index < length && i < 64; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
}
} else {
for (i = start;index < length && i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
}
}
}
bytes += i - start;
Expand Down
18 changes: 18 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ describe('sha256', function() {
expect(sha256('012345678012345678012345678012345678012345678012345678012345678012345678')).to.be('6fba9e623ae6abf028a1b195748814aa95eebfb22e3ec5e15d2444cd6c48186a');
});
});

describe('Array', function() {
describe('Array', function() {
it('should be successful', function() {
expect(sha256([])).to.be('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
expect(sha256([211, 212])).to.be('182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f');
expect(sha256([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])).to.be('d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592');
expect(sha256([48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55])).to.be('74b51c6911f9a8b5e7c499effe7604e43b672166818873c27752c248de434841');
});
});

describe('Uint8Array', function() {
it('should be successful', function() {
expect(sha256(new Uint8Array([]))).to.be('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
expect(sha256(new Uint8Array([211, 212]))).to.be('182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f');
});
});
});
});

describe('sha224', function() {
Expand Down

0 comments on commit f73267a

Please sign in to comment.