diff --git a/lib/utils/sha3.js b/lib/utils/sha3.js index f88c164..534651a 100644 --- a/lib/utils/sha3.js +++ b/lib/utils/sha3.js @@ -20,20 +20,18 @@ * @date 2015 */ - -var utils = require('./utils'); +var CryptoJS = require('crypto-js'); var sha3 = require('crypto-js/sha3'); -module.exports = function (str, isNew) { - if (str.substr(0, 2) === '0x' && !isNew) { - console.warn('requirement of using web3.fromAscii before sha3 is deprecated'); - console.warn('new usage: \'web3.sha3("hello")\''); - console.warn('see https://github.com/ethereum/web3.js/pull/205'); - console.warn('if you need to hash hex value, you can do \'sha3("0xfff", true)\''); - str = utils.toUtf8(str); +module.exports = function (value, options) { + if (options && options.encoding === 'hex') { + if (value.length > 2 && value.substr(0, 2) === '0x') { + value = value.substr(2); + } + value = CryptoJS.enc.Hex.parse(value); } - return sha3(str, { + return sha3(value, { outputLength: 256 }).toString(); }; diff --git a/test/sha3.js b/test/sha3.js index 7eeff8d..1e7503f 100644 --- a/test/sha3.js +++ b/test/sha3.js @@ -4,14 +4,15 @@ var sha3 = require('../lib/utils/sha3'); var web3 = require('../index'); describe('lib/utils/sha3', function () { - var test = function (v, e) { + var test = function (v, e, o) { it('should encode ' + v + ' to ' + e, function () { - assert.equal(sha3(v), e); + assert.equal(sha3(v, o), e); }); }; test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); - test(web3.fromUtf8('test123'), 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); + test('0x80', '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', { encoding: 'hex' }); + test('0x80', '6b03a5eef7706e3fb52a61c19ab1122fad7237726601ac665bd4def888f0e4a0'); });