sharedstreets / sharedstreets-builder

Making maps connectable: stable, non-proprietary IDs and data standards for streets
MIT License
28 stars 12 forks source link

Safe encoding in generateHash for hashInput #5

Closed DenisCarriere closed 6 years ago

DenisCarriere commented 6 years ago

Safe encoding in generateHash for hashInput

@kpwebb Noticed in the Java sharedstreets-builder UniqueId.java => generateHash method that were not encoding the hashInput, is that intended to not escape the spaces (%20)?

Example

const message = 'Intersection 110 45'
const messageSafe = encodeURI(message) // => 'Intersection%20110%2045'

// getBytes

// Same as SharedStreets Builder
getBytes(message) // => [73, 110, 116, 101, 114, 115, 101, 99, 116, 105, 111, 110, 32, 49, 49, 48, 32, 52, 53]

// Not the same
getBytes(messageSafe) // => [73, 110, 116, 101, 114, 115, 101, 99, 116, 105, 111, 110, 37, 50, 48, 49, 49, 48, 37, 50, 48, 52, 53]
DenisCarriere commented 6 years ago

Right now I've got a working generateHash method in NodeJS, it might be worth doing that for Python as well since the Remix Python implementation isn't the same as the Java/NodeJS.

import bs58 from 'bs58'

/**
 * Generates Hash for SharedStreets Reference ID
 *
 * @param {string} hashInput Hash Input
 * @returns {string} SharedStreets Reference ID
 * @example
 * sharedstreets.generateHash('Intersection 110 45')
 * // => 'NzUsPtY2FHmqaHuyaVzedp'
 */
export function generateHash (hashInput) {
  // https://github.com/sharedstreets/sharedstreets-builder/issues/5
  // hashInput = encodeURI(hashInput)
  var bytesOfMessage = getBytes(hashInput)
  var bytes = createHash('md5').update(Buffer.from(bytesOfMessage)).digest()
  return bs58.encode(bytes)
}

function getBytes (hashInput) {
  return Buffer.from(hashInput).toJSON()
}
DenisCarriere commented 6 years ago

Maybe I was thinking of something else, seems like the only thing that needs to happen is to confirm that the String is in UTF8.

java

byte[] bytesOfMessage = hashInput.getBytes("UTF-8");

node

const bytesOfMessage = Buffer.from(hashInput, 'utf8')

python

bytesOfMessage = hashInput.encode('utf8')