@frostoven/randomart

1.3.2 • Public • Published

randomart-generator

Generate OpenSSH style "randomart" images based on any data.

This project was originally based on a cli port of slapresta's work by therebelrobot. The original code was based on calmh/randomart, a randomart library written in Go. This project adds additional functionality.

All features:

  • Generate ASCII art from data such as cryptographic keys or other data.
  • Generate bitmaps images instead.
  • Optionally get raw data instead of conversion to visual data.
  • Override ASCII symbols for custom ASCII output.
  • Supports the browser (via bundling) and Node.js.

Installation

npm install @frostoven/randomart

Documentation

The function returned by require('@frostoven/randomart') optionally accepts two arguments: the first one, data, is expected to be an array of integers between 0 and 255, while the second one, options, has the following structure, in which all parent elements are optional.

Basic usage

Example:

const randomart = require('@frostoven/randomart')

console.log(randomart([
  0x9b, 0x4c, 0x7b, 0xce, 0x7a, 0xbd, 0x0a, 0x13,
  0x61, 0xfb, 0x17, 0xc2, 0x06, 0x12, 0x0c, 0xed,
]))

Result:

    .+.
      o.
     .. +
      Eo =
        S + .
       o B . .
        B o..
         *...
        .o+...

Overriding ASCII symbols

const randomart = require('@frostoven/randomart')

const options = {
  bounds: {
    width: 17,
    height: 9,
  },
  symbols: {
    '-2': 'E', // End
    '-1': 'S', // Start
    '0': ' ',
    '1': '.',
    // [...]
    '13': '/',
    '14': '^',
  },
  // This is false by default, set to true to get raw data instead of text.
  getRawData: false,
}

const randomartString = randomart(
  [/* your byte array */],
  options,
)

Generating a bitmap instead of text

Example:

const randomart = require('@frostoven/randomart')

const options = {
  bounds: {
    width: 48,
    height: 48,
  },
  asBitmap: true,
  darkMode: false,
}

const buffer = randomart(
  [/* 4096-bit RSA key */],
  options,
)
// If saving the image from Node.js
// ================================

const fs = require('fs')
const randomart = require('@frostoven/randomart')

const buffer = randomart(/* your options with `asBitmap: true` */)
fs.writeFile('/tmp/your_image.bmp', buffer, console.log)
// Generating an image inside from a browser
// =========================================

import randomart from '@frostoven/randomart'

function arrayBufferToString (ab) {
  // https://stackoverflow.com/questions/39725716/how-to-convert-javascript-array-to-binary-data-and-back-for-websocket
  return new Uint8Array(ab).reduce((p, c) => p + String.fromCharCode(c), '')
}

const buffer = randomart(/* your options with `asBitmap: true` */)
const data = `data:image/bmp;base64,${window.btoa(arrayBufferToString(buffer))}`

const img = document.getElementById('your-image')
img.src = data

Light mode example results:

example

example

Dark mode example results:

example

example

Credits

Thanks to @calmh for their hard work! May the opensource gods reward them with seventy-two non-terrible window managers.

Package Sidebar

Install

npm i @frostoven/randomart

Weekly Downloads

87

Version

1.3.2

License

MIT

Unpacked Size

42.6 kB

Total Files

12

Last publish

Collaborators

  • frostoven