Skip to content

Instantly share code, notes, and snippets.

@19h
Created October 1, 2018 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 19h/b2a7054fda269abe60945de5c3439abd to your computer and use it in GitHub Desktop.
Save 19h/b2a7054fda269abe60945de5c3439abd to your computer and use it in GitHub Desktop.
OpenSSH randomart
const defaultSymbols = 'ES .o+=*BOX@%&#/^'.split('');
const defaultBounds = {
width: 17,
height: 9
};
const generateBoard = (data, bounds) => {
/* create board */
const board = [];
for (var i = 0; i < bounds.width; i++) {
board[i] = [];
for (var j = 0; j < bounds.height; j++) {
board[i][j] = 2 /* empty */;
}
}
let x = bounds.width >> 1;
let y = bounds.height >> 1;
board[x][y] = 1 /* start */;
for (let i = 0; i < data.length; ++i) {
const b = data[i];
for (let s = 0; s < 8; s += 2) {
const d = (b >> s) & 3;
switch (d) {
case 0: // up
case 1:
if (y > 0) {
y--;
}
break;
case 2: // down
case 3:
if (y < (bounds.height - 1)) {
y++;
}
break;
}
switch (d) {
case 0: // left
case 2:
if (x > 0) {
x--;
}
break;
case 1: // right
case 3:
if (x < (bounds.width - 1)) {
x++;
}
break;
}
if (board[x][y] >= 2 /* empty */) {
board[x][y]++;
}
}
}
board[x][y] = 0 /* end */;
return {
board,
bounds
};
}
const randomart = (data, opts) => {
const options = opts || {};
const bounds = options.bounds || defaultBounds;
const symbols = options.symbols || defaultSymbols;
const board = generateBoard(data, bounds);
const width = board.bounds.width;
const height = board.bounds.height;
const result = [];
for (let i = 0; i < height; i++) {
result[i] = [];
for (let j = 0; j < width; j++) {
result[i][j] = symbols[board.board[j][i]] || symbols[2 /* empty */];
}
result[i] = result[i].join('');
}
return result.join('\n');
}
setInterval(() => {
const width = 17 * 2;
const height = 9 * 2;
let rart = randomart(
require('crypto').randomBytes(32),
{
bounds: {
width,
height
},
symbols: 'ES .o+=*BOX@%&#/^'.split('')
}
);
// +--[ RSA 2048 ]---+
const title = 'RSA 2048';
const titleBounds = title.length + 4;
const signBounds = 2;
const boundsOfMidchars = (width + 2) - titleBounds - signBounds;
const bOMH1 = boundsOfMidchars / 2;
const bOMH2 = boundsOfMidchars >> 1;
const isOdd = (boundsOfMidchars / 2) !== (boundsOfMidchars >> 1);
const leadingPad = '-'.repeat(bOMH2);
const trailingPad = '-'.repeat(isOdd ? (bOMH1 + 1) : bOMH2);
const titleChars = `+${leadingPad}[ ${title} ]${trailingPad}+`
const postambleChars = `+${'-'.repeat(width)}+`
const rartChars = rart.split('\n').map(line => `|${line}|`).join('\n');
process.stdout.write(
`\x1b[1;1H\r\x1b[0J\x1b[1G\x1b[1;1H\r${titleChars}\n${rartChars}\n${postambleChars}`
);
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment