From 548eaacf87f91fa5306286300a3e9739299aecf4 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 1 Apr 2025 12:43:43 -0400 Subject: [PATCH 1/2] main: refactor a bit to get logic out of main and into ./cmd/ Signed-off-by: Vincent Batts --- cmd/coreutils.go | 61 ++++++++++++++++++++++ cmd/emoji.go | 21 ++++++++ cmd/openssl.go | 68 ++++++++++++++++++++++++ main.go | 133 +++-------------------------------------------- 4 files changed, 157 insertions(+), 126 deletions(-) create mode 100644 cmd/coreutils.go create mode 100644 cmd/emoji.go create mode 100644 cmd/openssl.go diff --git a/cmd/coreutils.go b/cmd/coreutils.go new file mode 100644 index 0000000..6bf06a7 --- /dev/null +++ b/cmd/coreutils.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "bufio" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "strings" + + "github.com/kyokomi/emoji" +) + +/* +ParseCoreUtils expects some input like: +```shell +$ sha256sum ./tmp.efLuko +f18bd8b680e834ab8097a66deb0255821195d9624e39da6b65903ff6a09a01bb ./tmp.efLuko +``` +*/ +func ParseCoreUtils(line string) (filename string, sum []byte, err error) { + chunks := strings.SplitN(strings.TrimRight(line, "\n"), " ", 2) + if len(chunks) != 2 { + return "", nil, ErrNotCoreUtilsLine + } + sum, err = hex.DecodeString(chunks[0]) + if err != nil { + return "", nil, err + } + return chunks[1], sum, nil +} + +// ErrNotCoreUtilsLine when the line to parse is not formated like a coreutils checksum line +var ErrNotCoreUtilsLine = errors.New("not a coreutils checksum line") + +// PrintCoreUtils reads in the content like from `sha25sum ...` and returns +// in likeness but with emojisum instead. +// TODO(vb) return a buffer that the caller can choose to print out themselves +func PrintCoreUtils(rdr io.Reader) error { + buf := bufio.NewReader(rdr) + for { + line, err := buf.ReadString('\n') + if err != nil && err == io.EOF { + break + } else if err != nil { + return err + } + name, sum, err := ParseCoreUtils(line) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %q\n", err, line) + continue + } + str := EmojiFromBytes(sum) + fmt.Printf("%x %s\n", sum, name) + fmt.Printf("%s %s\n", str, name) + emoji.Print(str) + fmt.Printf(" %s\n", name) + } + return nil +} diff --git a/cmd/emoji.go b/cmd/emoji.go new file mode 100644 index 0000000..7907bcf --- /dev/null +++ b/cmd/emoji.go @@ -0,0 +1,21 @@ +package cmd + +import ( + esum "github.com/emojisum/emojisum/emoji" +) + +// EmojiFromBytes parses the bytes buffer for colon notation and returns the +// corresponding emoji +func EmojiFromBytes(buf []byte) string { + var ret string + for _, b := range buf { + for _, e := range esum.Map(b) { + // use the first colon notation word and continue + if esum.IsColonNotation(e) { + ret = ret + e + break + } + } + } + return ret +} diff --git a/cmd/openssl.go b/cmd/openssl.go new file mode 100644 index 0000000..b351d83 --- /dev/null +++ b/cmd/openssl.go @@ -0,0 +1,68 @@ +package cmd + +import ( + "bufio" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "strings" + + "github.com/kyokomi/emoji" +) + +/* +ParseOpenSSL expects some input like: +```shell +$> openssl sha256 tmp.efLuko +SHA256(tmp.efLuko)= f18bd8b680e834ab8097a66deb0255821195d9624e39da6b65903ff6a09a01bb +``` +*/ +func ParseOpenSSL(line string) (hash, filename string, sum []byte, err error) { + if !strings.Contains(line, "(") { + return "", "", nil, ErrNotOpenSSLLine + } + chunks := strings.SplitN(strings.TrimRight(line, "\n"), ")= ", 2) + if len(chunks) != 2 { + return "", "", nil, ErrNotOpenSSLLine + } + chunksprime := strings.SplitN(chunks[0], "(", 2) + if len(chunks) != 2 { + return "", "", nil, ErrNotOpenSSLLine + } + sum, err = hex.DecodeString(chunks[1]) + if err != nil { + return "", "", nil, err + } + return chunksprime[0], chunksprime[1], sum, nil +} + +// ErrNotOpenSSLLine when the line to parse is not formated like an OpenSSL checksum line +var ErrNotOpenSSLLine = errors.New("not an openssl checksum line") + +// PrintOpenSSL reads in the content like from `openssl sha25 ...` and returns +// in likeness but with emojisum instead. +// TODO(vb) return a buffer that the caller can choose to print out themselves +func PrintOpenSSL(rdr io.Reader) error { + buf := bufio.NewReader(rdr) + for { + line, err := buf.ReadString('\n') + if err != nil && err == io.EOF { + break + } else if err != nil { + return err + } + hash, name, sum, err := ParseOpenSSL(line) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %q\n", err, line) + continue + } + str := EmojiFromBytes(sum) + fmt.Printf("%s(%s)= %x\n", hash, name, sum) + fmt.Printf("%s(%s)= %s\n", hash, name, str) + fmt.Printf("%s(%s)= ", hash, name) + emoji.Println(str) + } + return nil +} diff --git a/main.go b/main.go index 06c57c2..5b3ad3f 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,11 @@ package main import ( - "bufio" - "crypto/sha1" - "encoding/hex" - "errors" "flag" "fmt" - "io" "os" - "strings" - esum "github.com/emojisum/emojisum/emoji" + "github.com/emojisum/emojisum/cmd" "github.com/kyokomi/emoji" ) @@ -31,58 +25,20 @@ func run() error { flag.Parse() if *flParseOpenSSL { - buf := bufio.NewReader(os.Stdin) - for { - line, err := buf.ReadString('\n') - if err != nil && err == io.EOF { - return nil - } else if err != nil { - return err - } - hash, name, sum, err := parseOpenSSL(line) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %q\n", err, line) - continue - } - str := emojiFromBytes(sum) - fmt.Printf("%s(%s)= %x\n", hash, name, sum) - fmt.Printf("%s(%s)= %s\n", hash, name, str) - fmt.Printf("%s(%s)= ", hash, name) - emoji.Println(str) - } - // never gets here because of the return on EOF or err + return cmd.PrintOpenSSL(os.Stdin) } if *flParseCoreUtils { - buf := bufio.NewReader(os.Stdin) - for { - line, err := buf.ReadString('\n') - if err != nil && err == io.EOF { - return nil - } else if err != nil { - return err - } - name, sum, err := parseCoreUtils(line) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %q\n", err, line) - continue - } - str := emojiFromBytes(sum) - fmt.Printf("%x %s\n", sum, name) - fmt.Printf("%s %s\n", str, name) - emoji.Print(str) - fmt.Printf(" %s\n", name) - } - // never gets here because of the return on EOF or err + return cmd.PrintCoreUtils(os.Stdin) } // Otherwise do the checksum ourselves if flag.NArg() == 0 { - sum, err := Sum(os.Stdin) + sum, err := cmd.Sum(os.Stdin) if err != nil { return err } - str := emojiFromBytes(sum) + str := cmd.EmojiFromBytes(sum) fmt.Printf("SHA1(-)= %x\n", sum) fmt.Printf("SHA1(-)= %s\n", str) fmt.Printf("SHA1(-)= ") @@ -97,11 +53,11 @@ func run() error { } defer fh.Close() - sum, err := Sum(fh) + sum, err := cmd.Sum(fh) if err != nil { return err } - str := emojiFromBytes(sum) + str := cmd.EmojiFromBytes(sum) fmt.Printf("SHA1(%s)= %x\n", arg, sum) fmt.Printf("SHA1(%s)= %s\n", arg, str) fmt.Printf("SHA1(%s)= ", arg) @@ -109,78 +65,3 @@ func run() error { } return nil } - -/* -openssl sum: -``` -$> openssl sha256 tmp.efLuko -SHA256(tmp.efLuko)= f18bd8b680e834ab8097a66deb0255821195d9624e39da6b65903ff6a09a01bb -``` -*/ -func parseOpenSSL(line string) (hash, filename string, sum []byte, err error) { - if !strings.Contains(line, "(") { - return "", "", nil, ErrNotOpenSSLLine - } - chunks := strings.SplitN(strings.TrimRight(line, "\n"), ")= ", 2) - if len(chunks) != 2 { - return "", "", nil, ErrNotOpenSSLLine - } - chunksprime := strings.SplitN(chunks[0], "(", 2) - if len(chunks) != 2 { - return "", "", nil, ErrNotOpenSSLLine - } - sum, err = hex.DecodeString(chunks[1]) - if err != nil { - return "", "", nil, err - } - return chunksprime[0], chunksprime[1], sum, nil -} - -// ErrNotOpenSSLLine when the line to parse is not formated like an OpenSSL checksum line -var ErrNotOpenSSLLine = errors.New("not an openssl checksum line") - -/* -coreutils output: -``` -$ sha256sum ./tmp.efLuko -f18bd8b680e834ab8097a66deb0255821195d9624e39da6b65903ff6a09a01bb ./tmp.efLuko -``` -*/ -func parseCoreUtils(line string) (filename string, sum []byte, err error) { - chunks := strings.SplitN(strings.TrimRight(line, "\n"), " ", 2) - if len(chunks) != 2 { - return "", nil, ErrNotCoreUtilsLine - } - sum, err = hex.DecodeString(chunks[0]) - if err != nil { - return "", nil, err - } - return chunks[1], sum, nil -} - -// ErrNotCoreUtilsLine when the line to parse is not formated like a coreutils checksum line -var ErrNotCoreUtilsLine = errors.New("not a coreutils checksum line") - -// Sum is a basic wrapper around crypto/sha1 -func Sum(r io.Reader) ([]byte, error) { - h := sha1.New() - if _, err := io.Copy(h, r); err != nil { - return nil, err - } - sum := h.Sum(nil) - return sum[:], nil -} - -func emojiFromBytes(buf []byte) string { - var ret string - for _, b := range buf { - for _, e := range esum.Map(b) { - // use the first colon notation word and continue - if esum.IsColonNotation(e) { - ret = ret + e - break - } - } - } - return ret -} From 864bb1e21016e292c721053f7163f5f607897ce9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 1 Apr 2025 13:52:01 -0400 Subject: [PATCH 2/2] README: correct the sentence and remove link to travis-ci Signed-off-by: Vincent Batts --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 475870d..6e29592 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Emojisum -[![Build Status](https://travis-ci.org/emojisum/emojisum.svg?branch=master)](https://travis-ci.org/emojisum/emojisum) -:pray: :paperclip: An easier way to compare hashes /fingerprints, when dealing human weak link :link: :tada: +:pray: :paperclip: An easier way to compare hashes /fingerprints, when dealing with the human weak link :link: :tada: A curated list of 256 emojis that are not entirely similar. Using http://www.webpagefx.com/tools/emoji-cheat-sheet/ and http://www.unicode.org/emoji/charts/full-emoji-list.html to compare them.