Skip to content

Instantly share code, notes, and snippets.

@Nircek
Forked from malexmave/randomart.html
Last active July 18, 2023 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nircek/a9d7ca533468512eb1fbd201b53d04e6 to your computer and use it in GitHub Desktop.
Save Nircek/a9d7ca533468512eb1fbd201b53d04e6 to your computer and use it in GitHub Desktop.
"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 :) -->
<!DOCTYPE html>
<html>
<body>
<div id="renderDiv" style='font-family:"Lucida Console", Monaco, monospace' />
<script type="text/javascript">
var Hash = Object.freeze({ MD5: "MD5", SHA256: "SHA256", AUTO: "" });
// ENTER YOUR FINGERPRINT HERE
var fp = "d4:d3:fd:ca:c4:d3:e9:94:97:cc:52:21:3b:e4:ba:e9";
var fp_hash = Hash.AUTO;
if (/^MD5:.*$/.test(fp)) fp = fp.substring(4), fp_hash = Hash.MD5;
else if (/^SHA256:.*$/.test(fp)) fp = fp.substring(7), fp_hash = Hash.SHA256;
else if (/^([0-9A-Fa-f]{2}:){15}[0-9A-Fa-f]{2}$/.test(fp)) fp_hash = Hash.MD5;
var fp_bytes = fp_hash == Hash.MD5 ? md5_to_bytes(fp) : sha256_to_bytes(fp);
var upper_desc = "[RSA 2048]";
var lower_desc = fp_hash == Hash.MD5 ? "[MD5]" : "[SHA256]";
function md5_to_bytes(fingerprint) {
return new Uint8Array(fingerprint.split(":").map(e => parseInt(e, 16)));
}
function sha256_to_bytes(fingerprint) {
fingerprint += "=".repeat(4 - (fingerprint.length % 4));
return new Uint8Array([...atob(fingerprint)].map(e => e.charCodeAt(0)));
}
function hyphen_padding(s, n = 17) {
return s.padStart(s.length + Math.floor((n - s.length) / 2), "-").padEnd(n, "-");
}
//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;
for (var i = 0; i < fp_bytes.length; i++) { // Iterate through words
var bin = reverse(pad(fp_bytes[i].toString(2), 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 = "+" + hyphen_padding(upper_desc) + "+<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 + "+" + hyphen_padding(lower_desc) + "+";
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment