cryptocoinjs / bs58

Base58 encoding/decoding for Bitcoin
http://cryptocoinjs.com/modules/misc/bs58/
MIT License
216 stars 48 forks source link

sometimes bs58 not decoding correctly #40

Closed chittim412 closed 4 years ago

chittim412 commented 4 years ago

Hello, hashcode is getting generated from murmurhash with uuid v4 when i trying to encode and decode sometimes it is working correctly and sometime working incorrectly

please follow the below code and correct and incorrect results

const bs58 = require('bs58') //"bs58": "^4.0.1", const murmur = require('murmurhash') //"murmurhash": "0.0.2",

const uuidv4= require('uuid/v4') //"uuid": "^3.3.3"

const hashcodeBefore = murmur(uuidv4()) console.log(hash code =>${hashcodeBefore}) const hexValueBeforeEncoding = hashcodeBefore.toString(16) console.log(hex value => ${hexValueBeforeEncoding})

// encode const encode = bs58.encode(Buffer.from(hexValueBeforeEncoding,'hex')) console.log(encode values is => ${encode})

//decode

const hexValueAfterDecoding = bs58.decode(encode).toString('hex'); console.log(decode to hex value => ${hexValueAfterDecoding})

const hashcodeAfter = parseInt(hexValueAfterDecoding,16) console.log(hash code => ${hashcodeAfter})

correct results 1) hash code is 2882525932 hex value => abcfdaec encode values is => 5PihXy decode to hex value => abcfdaec hash code => 2882525932 2) hash code =>842513527 hex value => 3237bc77 encode values is => 2HT6vJ decode to hex value => 3237bc77 hash code => 842513527

3) hash code =>4034442918 hex value => f078b6a6 encode values is => 79WaKf decode to hex value => f078b6a6 hash code => 4034442918

4) hash code =>3752366994 hex value => dfa89392 encode values is => 6iaryf decode to hex value => dfa89392 hash code => 3752366994

incorrect result 1) hash code =>205424363 hex value => c3e86eb encode values is => 28obT decode to hex value => c3e86e ( character b is missing here) hash code => 12839022 (incorrect hashcode value generated)

2) hash code =>70739630 hex value => 43766ae encode values is => PfH3 decode to hex value => 43766a (character e is missing here) hash code => 4421226 ( incorrect hascode value generated)

3) hash code =>66211204 hex value => 3f24d84 encode values is => ND9H decode to hex value => 3f24d8 (character 4 is missing here) hash code => 4138200 ( incorrect hascode value generated)

4) hash code =>189365373 hex value => b497c7d encode values is => 23fEW decode to hex value => b497c7 (character d is missing here) hash code => 11835335 ( incorrect hascode value generated)

is there any thing i am doing wrong in this code please check and let me know and thanks in advance

fanatid commented 4 years ago

It's happened because you create buffer not from aligned hex:

Buffer.from('c3e86eb', 'hex')
<Buffer c3 e8 6e>

b is missed in buffer.

You can solve it with String#padStart

const hex = 'c3e86eb'
Buffer.from(hex.padStart(hex.length + hex.length % 2, '0'), 'hex')
<Buffer 0c 3e 86 eb>