pax_global_header00006660000000000000000000000064130733443300014512gustar00rootroot0000000000000052 comment=b9badb8daabfa6c522815b7b897e25a49a02558d randomart-1.1.0/000077500000000000000000000000001307334433000135005ustar00rootroot00000000000000randomart-1.1.0/LICENSE000066400000000000000000000020421307334433000145030ustar00rootroot00000000000000Copyright (C) 2013 Jakob Borg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. randomart-1.1.0/README.md000066400000000000000000000013651307334433000147640ustar00rootroot00000000000000randomart [![Build Status](https://drone.io/github.com/calmh/randomart/status.png)](https://drone.io/github.com/calmh/randomart/latest) ========= Generate OpenSSH style "randomart" images based on key fingerprints. Documentation ------------- http://godoc.org/github.com/calmh/randomart Example ------- ```go data := []byte{ 0x9b, 0x4c, 0x7b, 0xce, 0x7a, 0xbd, 0x0a, 0x13, 0x61, 0xfb, 0x17, 0xc2, 0x06, 0x12, 0x0c, 0xed } ra := randomart.Generate(data, "RSA 2048") fmt.Println(ra) ``` ``` +--[ RSA 2048 ]---+ | .+. | | o. | | .. + | | Eo = | | S + . | | o B . . | | B o.. | | *... | | .o+... | +-----------------+ ``` License ------- MIT randomart-1.1.0/randomart.go000066400000000000000000000046441307334433000160260ustar00rootroot00000000000000// Package randomart generates OpenSSH style randomart images. package randomart import ( "bytes" ) // Dimensions of the generated image. const ( XDim = 17 YDim = 9 ) const ( start = -1 end = -2 ) // Board is a generated randomart board. type Board struct { tiles [YDim][XDim]int8 title string subtitle string } // Generate creates a Board to represent the given data by applying the drunken // bishop algorithm. func Generate(data []byte, title string) Board { return GenerateSubtitled(data, title, "") } func GenerateSubtitled(data []byte, title, subtitle string) Board { board := Board{title: title, subtitle: subtitle} var x, y int x = XDim / 2 y = YDim / 2 board.tiles[y][x] = start for _, b := range data { for s := uint(0); s < 8; s += 2 { d := (b >> s) & 3 switch d { case 0, 1: // Up if y > 0 { y-- } case 2, 3: // Down if y < YDim-1 { y++ } } switch d { case 0, 2: // Left if x > 0 { x-- } case 1, 3: // Right if x < XDim-1 { x++ } } if board.tiles[y][x] >= 0 { board.tiles[y][x]++ } } } if board.tiles[YDim/2][XDim/2] == 0 { board.tiles[YDim/2][XDim/2] = start } board.tiles[y][x] = end return board } // Returns the string representation of the Board, using the OpenSSH ASCII art // character set. func (board Board) String() string { var chars = []string{ " ", ".", "o", "+", "=", "*", "B", "O", "X", "@", "%", "&", "#", "/", "^", } var buf bytes.Buffer if len(board.title) > 15 { board.title = board.title[:15] } writeTitle(&buf, board.title) for _, row := range board.tiles { buf.WriteString("|") for _, c := range row { var s string if c == start { s = "S" } else if c == end { s = "E" } else if int(c) < len(chars) { s = chars[c] } else { s = chars[len(chars)-1] } buf.WriteString(s) } buf.WriteString("|\n") } writeTitle(&buf, board.subtitle) return buf.String() } func writeTitle(buf *bytes.Buffer, title string) { if title != "" { extraChars := len(title) + 2 - XDim if extraChars > 0 { title = title[:XDim-extraChars] } title = "[" + title + "]" } leftLen := (XDim - len(title)) / 2 rightLen := XDim - len(title) - leftLen buf.WriteString("+") for i := 0; i < leftLen; i++ { buf.WriteString("-") } buf.WriteString(title) for i := 0; i < rightLen; i++ { buf.WriteString("-") } buf.WriteString("+\n") } randomart-1.1.0/randomart_test.go000066400000000000000000000053631307334433000170640ustar00rootroot00000000000000package randomart import ( "strings" "testing" ) type TestCase struct { title string subtitle string data []byte clines []string } // Test cases taken from OpenSSH_5.9p1 output, modified header row to fix "RSA // 2048" text centering. var testcases = []TestCase{ { "RSA 2048", "", []byte{ 0x9b, 0x4c, 0x7b, 0xce, 0x7a, 0xbd, 0x0a, 0x13, 0x61, 0xfb, 0x17, 0xc2, 0x06, 0x12, 0x0c, 0xed, }, []string{ "+---[RSA 2048]----+", "| .+. |", "| o. |", "| .. + |", "| Eo = |", "| S + . |", "| o B . . |", "| B o.. |", "| *... |", "| .o+... |", "+-----------------+", }, }, { "RSA 2048", "", []byte{ 0x30, 0xaa, 0x88, 0x72, 0x7d, 0xc8, 0x30, 0xd0, 0x2b, 0x99, 0xc7, 0x8f, 0xd1, 0x86, 0x59, 0xfc, }, []string{ "+---[RSA 2048]----+", "| |", "| . . |", "|. . o o |", "| = * o o |", "|+ X + E S |", "|.+ @ . |", "|+ + = . |", "|.. . |", "| |", "+-----------------+", }, }, { "RSA 2048", "SHA256", []byte{ 0x30, 0xaa, 0x88, 0x72, 0x7d, 0xc8, 0x30, 0xd0, 0x2b, 0x99, 0xc7, 0x8f, 0xd1, 0x86, 0x59, 0xfc, }, []string{ "+---[RSA 2048]----+", "| |", "| . . |", "|. . o o |", "| = * o o |", "|+ X + E S |", "|.+ @ . |", "|+ + = . |", "|.. . |", "| |", "+----[SHA256]-----+", }, }, { "ED25519 256", "SHA256", []byte{ 0xa1, 0x37, 0xe4, 0xd4, 0xdf, 0xd2, 0xa0, 0x96, 0x1b, 0xc6, 0xf5, 0x9f, 0xf5, 0x34, 0x05, 0x80, 0xa7, 0xbd, 0x8f, 0x58, 0x3d, 0x55, 0x92, 0xff, 0x76, 0x1e, 0x4f, 0x6e, 0x30, 0xbb, 0x9f, 0x75, }, []string{ "+--[ED25519 256]--+", "| ..... |", "| .. . o..|", "| + .+o oo|", "| = o.=.= +|", "| . S B oo+o+|", "| . + oo.=+X|", "| .o o @E|", "| . . o O|", "| .=.|", "+----[SHA256]-----+", }, }, } func TestRandomart(t *testing.T) { for _, tc := range testcases { verify(t, tc.title, tc.subtitle, tc.data, tc.clines) } } func verify(t *testing.T, title string, subtitle string, data []byte, clines []string) { generated := strings.TrimSpace(GenerateSubtitled(data, title, subtitle).String()) glines := strings.Split(generated, "\n") if cl, gl := len(clines), len(glines); cl != gl { t.Errorf("Randomart length mismatch; %d != %d", gl, cl) } for i := range clines { if glines[i] != clines[i] { t.Errorf("Line %d mismatch %q != %q", i, glines[i], clines[i]) } } }