fixed #290, #320. Fixed sha3 encoding of hex values

This commit is contained in:
debris 2015-10-05 13:45:11 +02:00
parent da082a393c
commit b5f8244ff6
2 changed files with 12 additions and 13 deletions

View File

@ -20,20 +20,18 @@
* @date 2015 * @date 2015
*/ */
var CryptoJS = require('crypto-js');
var utils = require('./utils');
var sha3 = require('crypto-js/sha3'); var sha3 = require('crypto-js/sha3');
module.exports = function (str, isNew) { module.exports = function (value, options) {
if (str.substr(0, 2) === '0x' && !isNew) { if (options && options.encoding === 'hex') {
console.warn('requirement of using web3.fromAscii before sha3 is deprecated'); if (value.length > 2 && value.substr(0, 2) === '0x') {
console.warn('new usage: \'web3.sha3("hello")\''); value = value.substr(2);
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)\''); value = CryptoJS.enc.Hex.parse(value);
str = utils.toUtf8(str);
} }
return sha3(str, { return sha3(value, {
outputLength: 256 outputLength: 256
}).toString(); }).toString();
}; };

View File

@ -4,14 +4,15 @@ var sha3 = require('../lib/utils/sha3');
var web3 = require('../index'); var web3 = require('../index');
describe('lib/utils/sha3', function () { describe('lib/utils/sha3', function () {
var test = function (v, e) { var test = function (v, e, o) {
it('should encode ' + v + ' to ' + e, function () { it('should encode ' + v + ' to ' + e, function () {
assert.equal(sha3(v), e); assert.equal(sha3(v, o), e);
}); });
}; };
test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad');
test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1');
test(web3.fromUtf8('test123'), 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); test('0x80', '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', { encoding: 'hex' });
test('0x80', '6b03a5eef7706e3fb52a61c19ab1122fad7237726601ac665bd4def888f0e4a0');
}); });