Esempio n. 1
0
func run() error {
	if err := parseFlags(opts); err != nil {
		return err
	}

	// parse the given checksum
	expect, err := getInput(opts)
	if err != nil {
		return err
	}

	// have to read all of it before we output any of it :/
	input, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	// calculate the checksum of the input
	actual, err := mh.Sum(input, opts.AlgorithmCode, opts.Length)
	if err != nil {
		return err
	}

	// ensure checksums match
	if !bytes.Equal(expect, actual) {
		return mhopts.ErrMatch
	}

	// ok, checksums matched, write it out
	if !quiet {
		_, err = os.Stdout.Write(input)
	}
	return err
}
Esempio n. 2
0
// Multihash reads all the data in r and calculates its multihash.
func (o *Options) Multihash(r io.Reader) (mh.Multihash, error) {
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	return mh.Sum(b, o.AlgorithmCode, o.Length)
}
Esempio n. 3
0
func (id ID) Hash() mh.Multihash {
	// TODO: Use go-ipfs-util.DefaultIpfsHash
	//		 https://github.com/ipfs/go-ipfs-util/pull/1
	hash, err := mh.Sum(id.asBlockData(), mh.SHA2_256, -1)

	// Mulithash should only fail if an invalid len or code was passed.
	if err != nil {
		panic(err)
	}

	return hash
}
Esempio n. 4
0
func TestKey(t *testing.T) {

	h1, err := mh.Sum([]byte("beep boop"), mh.SHA2_256, -1)
	if err != nil {
		t.Error(err)
	}

	k1 := Key(h1)
	h2 := mh.Multihash(k1)
	k2 := Key(h2)

	if !bytes.Equal(h1, h2) {
		t.Error("Multihashes not equal.")
	}

	if k1 != k2 {
		t.Error("Keys not equal.")
	}
}