npm package: @frostoven/randomart
Description: OpenSSH randomart images library in JS, now with CLI!
Latest version: 1.3.2

# randomart-generator

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

This project was originally based on a cli port of
[slapresta](https://github.com/slapresta/randomart)'s work
by [therebelrobot](https://github.com/therebelrobot/randomart).
The original code was based on
[calmh/randomart](https://github.com/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

```bash
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:
```javascript
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

```javascript
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:
```javascript
const randomart = require('@frostoven/randomart')

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

const buffer = randomart(
  [/* 4096-bit RSA key */],
  options,
)
```

```javascript
// 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)
```

```javascript
// 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:

<img alt="example" height="48" src="example1a.bmp" width="48"/>
<img alt="example" height="48" src="example2a.bmp" width="48"/>

Dark mode example results:

<img alt="example" height="48" src="example1b.bmp" width="48"/>
<img alt="example" height="48" src="example2b.bmp" width="48"/>

## Credits

Thanks to [@calmh](https://github.com/calmh/randomart) for their hard work! May
the opensource gods reward them with seventy-two non-terrible window managers.