github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/asserts/digest_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package asserts_test 21 22 import ( 23 "crypto" 24 _ "crypto/sha256" 25 "encoding/base64" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/asserts" 30 ) 31 32 type encodeDigestSuite struct{} 33 34 var _ = Suite(&encodeDigestSuite{}) 35 36 func (eds *encodeDigestSuite) TestEncodeDigestOK(c *C) { 37 h := crypto.SHA512.New() 38 h.Write([]byte("some stuff to hash")) 39 digest := h.Sum(nil) 40 encoded, err := asserts.EncodeDigest(crypto.SHA512, digest) 41 c.Assert(err, IsNil) 42 43 decoded, err := base64.RawURLEncoding.DecodeString(encoded) 44 c.Assert(err, IsNil) 45 c.Check(decoded, DeepEquals, digest) 46 47 // sha3-384 48 b, err := base64.RawURLEncoding.DecodeString(blobSHA3_384) 49 c.Assert(err, IsNil) 50 encoded, err = asserts.EncodeDigest(crypto.SHA3_384, b) 51 c.Assert(err, IsNil) 52 c.Check(encoded, Equals, blobSHA3_384) 53 54 } 55 56 func (eds *encodeDigestSuite) TestEncodeDigestErrors(c *C) { 57 _, err := asserts.EncodeDigest(crypto.SHA1, nil) 58 c.Check(err, ErrorMatches, "unsupported hash") 59 60 _, err = asserts.EncodeDigest(crypto.SHA512, []byte{1, 2}) 61 c.Check(err, ErrorMatches, "hash digest by sha512 should be 64 bytes") 62 63 _, err = asserts.EncodeDigest(crypto.SHA3_384, []byte{1, 2}) 64 c.Check(err, ErrorMatches, "hash digest by sha3-384 should be 48 bytes") 65 }