From adf91dfe8f1a216ef43846bc4826b740a5def986 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sat, 16 May 2015 15:42:48 +0200 Subject: [PATCH] sha3 init --- bower.json | 3 ++- lib/utils/sha3.js | 30 ++++++++++++++++++++++++++++++ package.json | 1 + test/sha3.js | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/utils/sha3.js create mode 100644 test/sha3.js diff --git a/bower.json b/bower.json index ed62055..196d21c 100644 --- a/bower.json +++ b/bower.json @@ -8,7 +8,8 @@ "./dist/web3.min.js" ], "dependencies": { - "bignumber.js": ">=2.0.0" + "bignumber.js": ">=2.0.0", + "crypto-js": "~3.1.4" }, "repository": { "type": "git", diff --git a/lib/utils/sha3.js b/lib/utils/sha3.js new file mode 100644 index 0000000..0d13382 --- /dev/null +++ b/lib/utils/sha3.js @@ -0,0 +1,30 @@ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** + * @file sha3.js + * @author Marek Kotewicz + * @date 2015 + */ + +var sha3 = require('crypto-js/sha3'); + +module.exports = function (str) { + return sha3(str, { + outputLength: 256 + }).toString(); +}; + diff --git a/package.json b/package.json index 59d4f62..a481214 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "bignumber.js": "debris/bignumber.js#master", + "crypto-js": "^3.1.4", "xmlhttprequest": "*" }, "browser": { diff --git a/test/sha3.js b/test/sha3.js new file mode 100644 index 0000000..f72e146 --- /dev/null +++ b/test/sha3.js @@ -0,0 +1,15 @@ +var chai = require('chai'); +var assert = chai.assert; +var sha3 = require('../lib/utils/sha3'); + +describe('lib/utils/sha3', function () { + var test = function (v, e) { + it('should encode ' + v + ' to ' + e, function () { + assert.equal(sha3(v), e); + }); + }; + + test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); + test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); +}); +