]> gitweb.factorcode.org Git - factor.git/commitdiff
drunken-bishop: adding the OpenSSH "Drunken Bishop" ssh key visual fingerprint
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 28 Aug 2023 18:44:11 +0000 (11:44 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 28 Aug 2023 18:44:11 +0000 (11:44 -0700)
extra/drunken-bishop/authors.txt [new file with mode: 0644]
extra/drunken-bishop/drunken-bishop-tests.factor [new file with mode: 0644]
extra/drunken-bishop/drunken-bishop.factor [new file with mode: 0644]
extra/drunken-bishop/summary.txt [new file with mode: 0644]

diff --git a/extra/drunken-bishop/authors.txt b/extra/drunken-bishop/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/drunken-bishop/drunken-bishop-tests.factor b/extra/drunken-bishop/drunken-bishop-tests.factor
new file mode 100644 (file)
index 0000000..c35c015
--- /dev/null
@@ -0,0 +1,37 @@
+USING: drunken-bishop io.streams.string tools.test ;
+
+{ "\
++-----------------+
+|       .=o.  .   |
+|     . *+*. o    |
+|      =.*..o     |
+|       o + ..    |
+|        S o.     |
+|         o  .    |
+|          .  . . |
+|              o .|
+|               E.|
++-----------------+
+" } [
+    [
+        "fc94b0c1e5b0987c5843997697ee9fb7" drunken-bishop.
+    ] with-string-writer
+] unit-test
+
+{ "\
++-----------------+
+|       .=o.  .   |
+|     . *+*. o    |
+|      =.*..o     |
+|       o + ..    |
+|        S o.     |
+|         o  .    |
+|          .  . . |
+|              o .|
+|               E.|
++-----------------+
+" } [
+    [
+        "fc94b0c1e5b0987c5843997697ee9fb7" drunken-bishop.
+    ] with-string-writer
+] unit-test
diff --git a/extra/drunken-bishop/drunken-bishop.factor b/extra/drunken-bishop/drunken-bishop.factor
new file mode 100644 (file)
index 0000000..03d9ccb
--- /dev/null
@@ -0,0 +1,63 @@
+! Copyright (C) 2023 John Benediktsson
+! See https://factorcode.org/license.txt for BSD license
+
+USING: byte-arrays combinators endian io kernel math
+math.bitwise math.order math.parser namespaces sequences strings
+;
+
+IN: drunken-bishop
+
+<PRIVATE
+
+CONSTANT: SYMBOLS " .o+=*BOX@%&#/^SE"
+
+SYMBOL: board-width
+board-width [ 17 ] initialize
+
+SYMBOL: board-height
+board-height [ 9 ] initialize
+
+:: drunken-bishop ( bytes -- board )
+    board-width get :> w
+    board-height get :> h
+    h [ w <byte-array> ] replicate :> board
+    h 2/ :> y!
+    w 2/ :> x!
+
+    15 x y board nth set-nth
+
+    bytes [
+        { 0 -2 -4 -6 } [
+            shift 2 bits {
+                { 0b00 [ -1 -1 ] }
+                { 0b01 [ -1  1 ] }
+                { 0b10 [  1 -1 ] }
+                { 0b11 [  1  1 ] }
+            } case :> ( dy dx )
+            dy y + 0 h 1 - clamp y!
+            dx x + 0 w 1 - clamp x!
+            x y board nth [ 1 + 0 15 clamp ] change-nth
+        ] with each
+    ] each
+
+    16 x y board nth set-nth
+
+    board ;
+
+PRIVATE>
+
+GENERIC: drunken-bishop. ( n -- )
+
+M: string drunken-bishop.
+    hex-string>bytes drunken-bishop. ;
+
+M: integer drunken-bishop.
+    dup log2 8 /mod zero? [ 1 + ] unless >be drunken-bishop. ;
+
+M: byte-array drunken-bishop.
+    board-width get CHAR: - <string> "+" "+" surround [
+        print
+        drunken-bishop [
+            [ SYMBOLS nth ] "" map-as "|" "|" surround print
+        ] each
+    ] [ print ] bi ;
diff --git a/extra/drunken-bishop/summary.txt b/extra/drunken-bishop/summary.txt
new file mode 100644 (file)
index 0000000..0718b81
--- /dev/null
@@ -0,0 +1 @@
+Drunken bishop visual checksum algorithm