├── LICENSE ├── README.md └── b2sum.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2012 Dmitry Chestnykh 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | B2SUM 2 | ===== 3 | 4 | Utility to calculate BLAKE2 checksums written in Go. 5 | 6 | Note: The original utility available from is faster. 7 | 8 | Installation 9 | ------------ 10 | 11 | Download binary for your platform by clicking on the 12 | [Downloads](https://bitbucket.org/dchest/b2sum/downloads) item in the top menu, 13 | unzip it and put into your PATH. 14 | 15 | Available platforms: OS X (darwin), Linux, FreeBSD, Windows. 16 | 17 | To install from source, if you have Go installed: 18 | 19 | $ go get bitbucket.org/dchest/b2sum 20 | 21 | 22 | Usage 23 | ----- 24 | 25 | b2sum [-a=HASH] [-s=SIZE] [filename1] [filename2] ... 26 | 27 | Supported HASHes: blake2b, blake2s. 28 | 29 | If no filenames specified, reads from the standard input. 30 | 31 | 32 | Examples 33 | -------- 34 | 35 | $ echo -n "Hello world" | b2sum 36 | 6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183 37 | 38 | $ echo -n "Hello world" | b2sum -s=20 39 | 5ad31b81fc4dde5554e36af1e884d83ff5b24eb0 40 | 41 | $ b2sum -s=32 /bin/sh /etc/bashrc 42 | BLAKE2b-32 (/bin/sh) = 376f70f4acc6e204ed9d098ce0e93798cb7ed1b047686872c7f496d02364c85c 43 | BLAKE2b-32 (/etc/bashrc) = 1572d4fe68a18bae127fe79fa5d009fdb2e3357c238f722109012fa739aaacb7 44 | 45 | $ b2sum -s=20 FreeBSD-9.0-RELEASE-amd64-disc1.iso 46 | BLAKE2b-20 (FreeBSD-9.0-RELEASE-amd64-disc1.iso) = 4174862a104245d26b61315b80af92892f4a45f2 47 | 48 | $ b2sum -a=blake2s LICENSE 49 | BLAKE2s-32 (LICENSE) = c34533c95c63606f33b644eec52e58a3189aa65e133755b0ebec7a32d90d5736 50 | 51 | 52 | Packages 53 | -------- 54 | 55 | Pure Go implementations of BLAKE2 to use in your applicaitons: 56 | 57 | * BLAKE2b - 58 | * BLAKE2s - 59 | -------------------------------------------------------------------------------- /b2sum.go: -------------------------------------------------------------------------------- 1 | // b2sum command calculates BLAKE2 checksums of files. 2 | package main 3 | 4 | import ( 5 | "flag" 6 | "fmt" 7 | "hash" 8 | "io" 9 | "os" 10 | 11 | "github.com/dchest/blake2b" 12 | "github.com/dchest/blake2s" 13 | ) 14 | 15 | var ( 16 | algoFlag = flag.String("a", "blake2b", "hash algorithm (blake2b, blake2s)") 17 | sizeFlag = flag.Int("s", 0, "digest size in bytes (0 = default)") 18 | ) 19 | 20 | type hashDesc struct { 21 | name string 22 | maxSize int 23 | maker func(size uint8) (hash.Hash, error) 24 | } 25 | 26 | var algorithms = map[string]hashDesc{ 27 | "blake2b": { 28 | "BLAKE2b", 29 | blake2b.Size, 30 | func(size uint8) (hash.Hash, error) { return blake2b.New(&blake2b.Config{Size: size}) }, 31 | }, 32 | "blake2s": { 33 | "BLAKE2s", 34 | blake2s.Size, 35 | func(size uint8) (hash.Hash, error) { return blake2s.New(&blake2s.Config{Size: size}) }, 36 | }, 37 | } 38 | 39 | func calcSum(f *os.File, h hash.Hash) (sum []byte, err error) { 40 | h.Reset() 41 | _, err = io.Copy(h, f) 42 | sum = h.Sum(nil) 43 | return 44 | } 45 | 46 | func main() { 47 | flag.Parse() 48 | 49 | algo, ok := algorithms[*algoFlag] 50 | if !ok { 51 | flag.Usage() 52 | fmt.Fprintf(os.Stderr, `unsupported algorithm: %s`, *algoFlag) 53 | os.Exit(1) 54 | } 55 | if *sizeFlag == 0 { 56 | *sizeFlag = algo.maxSize 57 | } else if *sizeFlag > algo.maxSize { 58 | fmt.Fprintf(os.Stderr, "error: size too large") 59 | os.Exit(1) 60 | } 61 | 62 | h, err := algo.maker(uint8(*sizeFlag)) 63 | if err != nil { 64 | fmt.Fprintf(os.Stderr, "error: %s", err) 65 | os.Exit(1) 66 | } 67 | 68 | if flag.NArg() == 0 { 69 | // Read from stdin. 70 | sum, err := calcSum(os.Stdin, h) 71 | if err != nil { 72 | fmt.Fprintf(os.Stderr, "%s\n", err) 73 | os.Exit(1) 74 | } 75 | fmt.Printf("%x\n", sum) 76 | os.Exit(0) 77 | } 78 | exitNo := 0 79 | for i := 0; i < flag.NArg(); i++ { 80 | filename := flag.Arg(i) 81 | f, err := os.Open(filename) 82 | if err != nil { 83 | fmt.Fprintf(os.Stderr, "(%s) %s\n", filename, err) 84 | exitNo = 1 85 | continue 86 | } 87 | sum, err := calcSum(f, h) 88 | f.Close() 89 | if err != nil { 90 | fmt.Fprintf(os.Stderr, "(%s) %s\n", filename, err) 91 | exitNo = 1 92 | continue 93 | } 94 | fmt.Printf("%s-%d (%s) = %x\n", algo.name, h.Size(), filename, sum) 95 | } 96 | os.Exit(exitNo) 97 | } 98 | --------------------------------------------------------------------------------