Skip to content

Instantly share code, notes, and snippets.

@malexmave

malexmave/randomart.html

Last active Aug 20, 2020
Embed
What would you like to do?
"Drunk Bishop" RandomArt algorithm (from SSH), implemented as Javascript. Only demo code here, but can be trivially changed to give some return value to do something with the randomart.
<!-- I was recently asked what license this code is under. Since it is fairly trivial code,
I don't really feel it "deserves" a "real" license, so I am hereby placing it in the public domain.
In countries where this is not posible, I am placing it under Creative Commons CC0
(https://creativecommons.org/publicdomain/zero/1.0/), which is basically just a more formal way of
putting it in the public domain for people who like to be sure.
If you want to credit me anyway, that is of course fine with me :) -->
<html>
<body>
<div id="renderDiv" style='font-family:"Lucida Console", Monaco, monospace' />
<script type="text/javascript">
// ENTER YOUR FINGERPRINT HERE
var fp = "d4:d3:fd:ca:c4:d3:e9:94:97:cc:52:21:3b:e4:ba:e9";
//Useful Functions
function checkHex(n){return/^[0-9A-Fa-f]{1,64}$/.test(n)}
function pad(s,z){s=""+s;return s.length<z?pad("0"+s,z):s}
//Hexadecimal Operations
function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
function reverse(s) {
return s.split("").reverse().join("");
}
function isValidMove(x, y) {
return (0 <= x && x <= 16 && 0 <= y && y <= 8);
}
var map = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
var x = 8;
var y = 4;
var arr = fp.split(":"); // Split fingerprint into 8-bit words
for (var i = 0; i < 16; i++) { // Iterate through words
var bin = reverse(pad(Hex2Bin(arr[i]), 8)); // Convert to binary
for (var c = 0; c < 8; c = c + 2) { // Iterate through the bit-pairs in the words
var move = reverse(bin.substring(c, c + 2)); // determine the move
switch (move) {
case "00":
if (isValidMove(x - 1, y - 1)) {
x--;
y--;
} else if (isValidMove(x - 1, y)) {
x--;
} else if (isValidMove(x, y - 1)) {
y--;
}
break;
case "01":
if (isValidMove(x + 1, y - 1)) {
x++;
y--;
} else if (isValidMove(x + 1, y)) {
x++;
} else if (isValidMove(x, y - 1)) {
y--;
}
break;
case "10":
if (isValidMove(x - 1, y + 1)) {
x--;
y++;
} else if (isValidMove(x - 1, y)) {
x--;
} else if (isValidMove(x, y + 1)) {
y++;
}
break;
case "11":
if (isValidMove(x + 1, y + 1)) {
x++;
y++;
} else if (isValidMove(x + 1, y)) {
x++;
} else if (isValidMove(x, y + 1)) {
y++;
}
break;
default:
alert("WTF!");
}
map[y][x]++; // increment the value at the new position
}
}
map[y][x] = -1; // Set end position
map[4][8] = 255; // Re-set start position
// The following will render the visual fingerprint
var div = document.getElementById('renderDiv');
div.innerHTML = "+--[ RSA 2048]----+<br>";
for (var i = 0; i <= 8; i++) {
div.innerHTML = div.innerHTML + "|";
for (var j = 0; j <= 16; j++) {
var char = "&nbsp;";
switch (map[i][j]) {
case 1:
char = ".";
break;
case 2:
char = "o";
break;
case 3:
char = "+";
break;
case 4:
char = "=";
break;
case 5:
char = "*";
break;
case 6:
char = "B";
break;
case 7:
char = "O";
break;
case 8:
char = "X";
break;
case 9:
char = "@";
break;
case 10:
char = "%";
break;
case 11:
char = "&";
break;
case 12:
char = "#";
break;
case 13:
char = "/";
break;
case 14:
char = "^";
break;
case 255:
char = "S";
break;
case -1:
char = "E";
break;
default:
char = "&nbsp;";
}
div.innerHTML = div.innerHTML + char;
}
div.innerHTML = div.innerHTML + "|<br>";
}
div.innerHTML = div.innerHTML + "+-----------------+";
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment