create indenticon-like visual hashes styled like Github and Gravatar (retro) avatars


create indenticon-like image hashes styled like Github and Gravatar (retro) avatars

var retricon = require('retricon');
var fmt = require('util').format;

console.log(fmt("<img alt='kibo' src='%s' />", retricon('kibo', 16, 0).toDataURL()));

  • str: string - username, email, or other string to hash
  • pixelSize: int - size in pixels to render each virtual pixel
  • border: int (optional) - number of pixels padding about each rendered pixel. This is inside the bounds defined by pixelSize.

Returns a Canvas object containing the rendered image.

The input string is converted to a buffer of UTF-8 bytes. A one-byte iterator is appended to this, initialized at zero. The buffer is passed through SHA-512, and the output buffer is iteratively XORed to produce a final buffer of only the number of required bytes (currently eighteen). The first three bytes are taken as RGB values for the colour. The remaining fifteen bytes are quantized to boolean values at a threshold of 127. If at least 5 of the booleans are true, the hash is accepted. Otherwise, the iterator is incremented, and the process repeated.

The array of booleans is taken to be a 3x5 row-major array. It is reflected about the x=2 axis to for a 5x5 array, which is then rendered as the final visual hash.