├── .gobuilder.yml ├── LICENSE ├── README.md ├── hashpipe ├── hashpipe.go └── install.sh /.gobuilder.yml: -------------------------------------------------------------------------------- 1 | --- 2 | artifacts: 3 | - LICENSE 4 | - README.md 5 | - install.sh 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Juan Batiz-Benet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hashpipe - pipe iff the hash matches 2 | 3 | `hashpipe` helps you venture into the unknown. It reads from stdin, checks the hash of the content, and outputs it IF AND ONLY IF it matches the provided hash checksum. This makes executing things a teensy bit safer, as it requires compromising more communication channels. 4 | 5 | ## Example 6 | 7 | ```sh 8 | > echo "hello" | multihash 9 | QmUJPTFZnR2CPGAzmfdYPghgrFtYFB6pf1BqMvqfiPDam8 10 | > echo "hello" | hashpipe QmUJPTFZnR2CPGAzmfdYPghgrFtYFB6pf1BqMvqfiPDam8 11 | hello 12 | > echo "goodbye" | hashpipe QmUJPTFZnR2CPGAzmfdYPghgrFtYFB6pf1BqMvqfiPDam8 13 | error: multihash checksums did not match 14 | ``` 15 | 16 | ## Use Case 17 | 18 | If you tell people to pipe things directly into the shell... Don't do that. If you're going to, _at least_ provide them a hash to ensure that man-in-the-middle attacks or compromised CDNs do not hurt your users. 19 | 20 | ``` 21 | curl http://you.shouldnt.be/doing/this | hashpipe Qmepk1VCHpjyCmWeh61vyDKsWfKymyrBQLcmUpXUdUd3yM | sh 22 | ``` 23 | 24 | ## Hashes Supported 25 | 26 | `hashpipe` uses [multihash](https://github.com/jbenet/multihash/), a self-describing hash function. It supports all the hashes in [go-multihash](https://github.com/jbenet/go-multihash/): 27 | 28 | - sha1 29 | - sha2-256 30 | - sha2-512 31 | - sha3-256 32 | - sha3-512 33 | - [add the one you need](https://github.com/jbenet/go-multihash/pulls) 34 | 35 | ## Install 36 | 37 | - From source: 38 | 39 | ``` 40 | go get github.com/jbenet/hashpipe 41 | ``` 42 | 43 | - Pre-built binaries: https://gobuilder.me/github.com/jbenet/hashpipe 44 | 45 | ## Usage 46 | 47 | ``` 48 | > hashpipe -h 49 | usage: hashpipe [MULTIHASH] <[FILE] >[FILE] 50 | 51 | cat untrustedFile | hashpipe | trustedContext 52 | 53 | hashpipe - boldly journey into the unknown. 54 | 55 | It reads from stdin, checks the hash of the content, and outputs it IF AND 56 | ONLY IF it matches the provided hash checksum. This makes executing things 57 | a bit safer, as it requires compromising more communication channels. On 58 | error, hashpipe returns a non-zero error code, failing pipelines. 59 | 60 | OPTIONS 61 | -a="sha2-256": one of: sha1, sha2-256, sha2-512, sha3 (shorthand) 62 | -algorithm="sha2-256": one of: sha1, sha2-256, sha2-512, sha3 63 | -e="base58": one of: raw, hex, base58, base64 (shorthand) 64 | -encoding="base58": one of: raw, hex, base58, base64 65 | -l=-1: checksums length in bits (truncate). -1 is default (shorthand) 66 | -length=-1: checksums length in bits (truncate). -1 is default 67 | -q=false: quiet output (no newline on checksum, no error text) (shorthand) 68 | -quiet=false: quiet output (no newline on checksum, no error text) 69 | ``` 70 | -------------------------------------------------------------------------------- /hashpipe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbenet/hashpipe/473d85eb833fee8ec54654e139b40c0172555137/hashpipe -------------------------------------------------------------------------------- /hashpipe.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "flag" 6 | "fmt" 7 | "io/ioutil" 8 | "os" 9 | 10 | mh "github.com/jbenet/go-multihash" 11 | mhopts "github.com/jbenet/go-multihash/opts" 12 | ) 13 | 14 | var usage = `usage: %s [MULTIHASH] <[FILE] >[FILE] 15 | 16 | cat untrustedFile | hashpipe | trustedContext 17 | 18 | hashpipe - boldly journey into the unknown. 19 | 20 | It reads from stdin, checks the hash of the content, and outputs it IF AND 21 | ONLY IF it matches the provided hash checksum. This makes executing things 22 | a bit safer, as it requires compromising more communication channels. On 23 | error, hashpipe returns a non-zero error code, failing pipelines. 24 | 25 | OPTIONS 26 | ` 27 | 28 | // flags 29 | var opts *mhopts.Options 30 | var quiet bool 31 | 32 | func init() { 33 | flag.Usage = func() { 34 | fmt.Fprintf(os.Stderr, usage, os.Args[0]) 35 | flag.PrintDefaults() 36 | } 37 | 38 | opts = mhopts.SetupFlags(flag.CommandLine) 39 | 40 | quietStr := "quiet output (no newline on checksum, no error text)" 41 | flag.BoolVar(&quiet, "quiet", false, quietStr) 42 | flag.BoolVar(&quiet, "q", false, quietStr+" (shorthand)") 43 | } 44 | 45 | func parseFlags(o *mhopts.Options) error { 46 | flag.Parse() 47 | if err := o.ParseError(); err != nil { 48 | return err 49 | } 50 | return nil 51 | } 52 | 53 | func getInput(o *mhopts.Options) (mh.Multihash, error) { 54 | args := flag.Args() 55 | if len(args) < 1 { 56 | return nil, fmt.Errorf("multihash is a required argument") 57 | } 58 | raw := args[0] 59 | 60 | h, err := mhopts.Decode(o.Encoding, raw) 61 | if err != nil { 62 | return nil, fmt.Errorf("fail to decode multihash '%s': %s", raw, err) 63 | } 64 | return h, nil 65 | } 66 | 67 | func run() error { 68 | if err := parseFlags(opts); err != nil { 69 | return err 70 | } 71 | 72 | // parse the given checksum 73 | expect, err := getInput(opts) 74 | if err != nil { 75 | return err 76 | } 77 | 78 | // have to read all of it before we output any of it :/ 79 | input, err := ioutil.ReadAll(os.Stdin) 80 | if err != nil { 81 | return err 82 | } 83 | 84 | // calculate the checksum of the input 85 | actual, err := mh.Sum(input, opts.AlgorithmCode, opts.Length) 86 | if err != nil { 87 | return err 88 | } 89 | 90 | // ensure checksums match 91 | if !bytes.Equal(expect, actual) { 92 | return mhopts.ErrMatch 93 | } 94 | 95 | // ok, checksums matched, write it out 96 | if !quiet { 97 | _, err = os.Stdout.Write(input) 98 | } 99 | return err 100 | } 101 | 102 | func main() { 103 | if err := run(); err != nil { 104 | if !quiet { 105 | fmt.Fprintf(os.Stderr, "error: %s\n", err) 106 | } 107 | os.Exit(1) 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | bin=hashpipe 4 | 5 | # this script is currently brain dead. 6 | # it merely tries two locations. 7 | # in the future maybe use value of $PATH. 8 | 9 | binpath=/usr/local/bin 10 | if [ -d "$binpath" ]; then 11 | mv "$bin" "$binpath/$bin" 12 | echo "installed $binpath/$bin" 13 | exit 0 14 | fi 15 | 16 | binpath=/usr/bin 17 | if [ -d "$binpath" ]; then 18 | mv "$bin" "$binpath/$bin" 19 | echo "installed $binpath/$bin" 20 | exit 0 21 | fi 22 | --------------------------------------------------------------------------------