本文整理汇总了Golang中golang.org/x/crypto/sha3.New256函数的典型用法代码示例。如果您正苦于以下问题:Golang New256函数的具体用法?Golang New256怎么用?Golang New256使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New256函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseBlob
// Initialize manifest from BLOB
func (s *Manifest) ParseBlob(in io.Reader, chunkSize int64) (err error) {
var chunkHasher hash.Hash
var w io.Writer
var chunk Chunk
hasher := sha3.New256()
var written int64
for {
chunk = Chunk{
Offset: s.Size,
}
chunkHasher = sha3.New256()
w = io.MultiWriter(hasher, chunkHasher)
written, err = io.CopyN(w, in, chunkSize)
s.Size += written
chunk.Size = written
chunk.ID = ID(hex.EncodeToString(chunkHasher.Sum(nil)))
s.Chunks = append(s.Chunks, chunk)
if err == io.EOF {
err = nil
break
} else if err != nil {
return
}
}
s.ID = ID(hex.EncodeToString(hasher.Sum(nil)))
return
}
示例2: Test_SHA3
func Test_SHA3(t *testing.T) {
hasher1 := sha3.New256()
hasher1.Write([]byte("mama_myla_ramu"))
hasher2 := sha3.New256()
hasher2.Write([]byte("mama_"))
hasher2.Write([]byte("myla_"))
hasher2.Write([]byte("ramu"))
assert.Equal(t, hasher1.Sum(nil), hasher2.Sum(nil))
}
示例3: getCAFile
func (s *BlockStorage) getCAFile(name string) (w *contentaddressable.File, err error) {
caOpts := contentaddressable.DefaultOptions()
caOpts.Hasher = sha3.New256()
w, err = contentaddressable.NewFileWithOptions(name, caOpts)
return
}
示例4: Checksum
// Checksum returns the checksum of some data, using a specified algorithm.
// It only returns an error when an invalid algorithm is used. The valid ones
// are MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA3224, SHA3256, SHA3384,
// and SHA3512.
func Checksum(algorithm string, data []byte) (checksum string, err error) {
// default
var hasher hash.Hash
switch strings.ToUpper(algorithm) {
case "MD5":
hasher = md5.New()
case "SHA1":
hasher = sha1.New()
case "SHA224":
hasher = sha256.New224()
case "SHA256":
hasher = sha256.New()
case "SHA384":
hasher = sha512.New384()
case "SHA512":
hasher = sha512.New()
case "SHA3224":
hasher = sha3.New224()
case "SHA3256":
hasher = sha3.New256()
case "SHA3384":
hasher = sha3.New384()
case "SHA3512":
hasher = sha3.New512()
default:
msg := "Invalid algorithm parameter passed go Checksum: %s"
return checksum, fmt.Errorf(msg, algorithm)
}
hasher.Write(data)
str := hex.EncodeToString(hasher.Sum(nil))
return str, nil
}
示例5: PutData3
func (u *U16x16) PutData3(data []byte, key string) (length int64, hash string, err error) {
s := sha3.New256()
s.Write(data)
hash = hex.EncodeToString(s.Sum(nil))
if hash != key {
fmt.Printf("expected data to have key %s, but content key is %s",
key, hash)
err = errors.New("content/key mismatch")
return
}
length = int64(len(data))
topSubDir := hash[0:1]
lowerDir := hash[1:2]
targetDir := filepath.Join(u.path, topSubDir, lowerDir)
found, err := xf.PathExists(targetDir)
if err == nil && !found {
err = os.MkdirAll(targetDir, 0775)
}
fullishPath := filepath.Join(targetDir, key[2:])
found, err = xf.PathExists(fullishPath)
if !found {
var dest *os.File
dest, err = os.Create(fullishPath)
if err == nil {
var count int
defer dest.Close()
count, err = dest.Write(data)
if err == nil {
length = int64(count)
}
}
}
return
}
示例6: PutData3
func (u *UFlat) PutData3(data []byte, key string) (length int64, hash string, err error) {
s := sha3.New256()
s.Write(data)
hash = hex.EncodeToString(s.Sum(nil))
if hash != key {
fmt.Printf("expected data to have key %s, but content key is %s",
key, hash)
err = errors.New("content/key mismatch")
return
}
length = int64(len(data))
fullishPath := filepath.Join(u.path, key)
found, err := xf.PathExists(fullishPath)
if !found {
var dest *os.File
dest, err = os.Create(fullishPath)
if err == nil {
var count int
defer dest.Close()
count, err = dest.Write(data)
if err == nil {
length = int64(count)
}
}
}
return
}
示例7: Test_Assembler_StoreChunk
func Test_Assembler_StoreChunk(t *testing.T) {
wd, _ := os.Getwd()
wd = filepath.Join(wd, "testdata", "assembler-StoreChunk")
m, err := model.New(wd, false, proto.CHUNK_SIZE, 128)
assert.NoError(t, err)
data := []byte("mama myla ramu")
hasher := sha3.New256()
_, err = hasher.Write([]byte(data))
id := proto.ID(hex.EncodeToString(hasher.Sum(nil)))
a, err := model.NewAssembler(m)
assert.NoError(t, err)
defer a.Close()
err = a.StoreChunk(bytes.NewReader(data), id)
assert.NoError(t, err)
// check stored chunk
f, err := os.Open(filepath.Join(a.Where, id.String()))
assert.NoError(t, err)
defer f.Close()
defer os.Remove(filepath.Join(a.Where, id.String()))
r2, err := ioutil.ReadAll(f)
assert.NoError(t, err)
assert.Equal(t, data, r2)
}
示例8: verifyLeafSHA
func (s *XLSuite) verifyLeafSHA(c *C, rng *xr.PRNG,
node MerkleNodeI, pathToFile string, whichSHA int) {
c.Assert(node.IsLeaf(), Equals, true)
found, err := xf.PathExists(pathToFile)
c.Assert(err, IsNil)
c.Assert(found, Equals, true)
data, err := ioutil.ReadFile(pathToFile)
c.Assert(err, IsNil)
c.Assert(data, NotNil)
var sha hash.Hash
switch whichSHA {
case xu.USING_SHA1:
sha = sha1.New()
case xu.USING_SHA2:
sha = sha256.New()
case xu.USING_SHA3:
sha = sha3.New256()
// XXX DEFAULT = ERROR
}
sha.Write(data)
sum := sha.Sum(nil)
c.Assert(node.GetHash(), DeepEquals, sum)
}
示例9: StoreChunk
// Store chunk in assemble
func (a *Assembler) StoreChunk(r io.Reader, id proto.ID) (err error) {
lock, err := a.model.FdLocks.Take()
if err != nil {
return
}
defer lock.Release()
caOpts := contentaddressable.DefaultOptions()
caOpts.Hasher = sha3.New256()
w, err := contentaddressable.NewFileWithOptions(
filepath.Join(a.Where, id.String()), caOpts)
if os.IsExist(err) {
err = nil
return
}
if err != nil {
return
}
defer w.Close()
if _, err = io.Copy(w, r); err != nil {
return
}
err = w.Accept()
return
}
示例10: SchnorrVerify
// Checks the signature against
// the message
func SchnorrVerify(suite abstract.Suite,
kp SchnorrPublicKey,
msg []byte, sig []byte) (bool, error) {
buf := bytes.NewBuffer(sig)
signature := SchnorrSignature{}
err := abstract.Read(buf, &signature, suite)
if err != nil {
return false, err
}
s := signature.S
e := signature.E
var gs, ye, r abstract.Point
gs = suite.Point().Mul(nil, s) // g^s
ye = suite.Point().Mul(kp.Y, e) // y^e
r = suite.Point().Add(gs, ye) // g^xy^e
r_bin, _ := r.MarshalBinary()
msg_and_r := append(msg, r_bin...)
hasher := sha3.New256()
hasher.Write(msg_and_r)
h := hasher.Sum(nil)
// again I'm hoping this just reads the state out
// and doesn't actually perform any ops
lct := suite.Cipher(h)
ev := suite.Secret().Pick(lct)
return ev.Equal(e), nil
}
示例11: GenerateZ
/* GenerateZ takes some random agreed information and creates
Z the "public-only" key that is witness-independent as per
the paper. We've probably broken that slightly in this implementation
because I could not pick a point without generating it
via a Secret, instead of directly via a Point - that is, even as a
32-byte string, we cannot decode on C25519 (and this wouldn't work
for abstract suites anyway).
However, it demonstrates the idea.
*/
func GenerateZ(suite abstract.Suite, info []byte) (abstract.Point, error) {
hasher := sha3.New256()
hasher.Write(info)
zraw := hasher.Sum(nil)
//I think this might be cheating
zrawCt := suite.Cipher(zraw)
zfactor := suite.Secret().Pick(zrawCt)
Z := suite.Point()
Z.Mul(nil, zfactor)
// every 32-bit integer exists on Curve25519 only if we have the fullgroup
// this should work, but doesn't.
/*var Z abstract.Point
zrawBuf := bytes.NewBuffer(zraw)
err := abstract.Read(zrawBuf, &Z, suite);
if err != nil {
return nil, err
}*/
return Z, nil
}
示例12: TestHashOpts
func TestHashOpts(t *testing.T) {
msg := []byte("abcd")
// SHA256
digest1, err := currentBCCSP.Hash(msg, &bccsp.SHA256Opts{})
if err != nil {
t.Fatalf("Failed computing SHA256 [%s]", err)
}
h := sha256.New()
h.Write(msg)
digest2 := h.Sum(nil)
if !bytes.Equal(digest1, digest2) {
t.Fatalf("Different SHA256 computed. [%x][%x]", digest1, digest2)
}
// SHA384
digest1, err = currentBCCSP.Hash(msg, &bccsp.SHA384Opts{})
if err != nil {
t.Fatalf("Failed computing SHA384 [%s]", err)
}
h = sha512.New384()
h.Write(msg)
digest2 = h.Sum(nil)
if !bytes.Equal(digest1, digest2) {
t.Fatalf("Different SHA384 computed. [%x][%x]", digest1, digest2)
}
// SHA3_256O
digest1, err = currentBCCSP.Hash(msg, &bccsp.SHA3_256Opts{})
if err != nil {
t.Fatalf("Failed computing SHA3_256 [%s]", err)
}
h = sha3.New256()
h.Write(msg)
digest2 = h.Sum(nil)
if !bytes.Equal(digest1, digest2) {
t.Fatalf("Different SHA3_256 computed. [%x][%x]", digest1, digest2)
}
// SHA3_384
digest1, err = currentBCCSP.Hash(msg, &bccsp.SHA3_384Opts{})
if err != nil {
t.Fatalf("Failed computing SHA3_384 [%s]", err)
}
h = sha3.New384()
h.Write(msg)
digest2 = h.Sum(nil)
if !bytes.Equal(digest1, digest2) {
t.Fatalf("Different SHA3_384 computed. [%x][%x]", digest1, digest2)
}
}
示例13: TestXLatticePkt
func (d *XLSuite) TestXLatticePkt(c *C) {
if VERBOSITY > 0 {
fmt.Println("TEST_XLATTICE_PKT")
}
rng := xr.MakeSimpleRNG()
myMsgN := uint64(rng.Int63())
for myMsgN == 0 { // must not be zero
myMsgN = uint64(rng.Int63())
}
id := make([]byte, 32) // sha3 length
rng.NextBytes(id) // random bytes
seqBuf := new(bytes.Buffer)
binary.Write(seqBuf, binary.LittleEndian, myMsgN)
msgLen := 64 + rng.Intn(64)
msg := make([]byte, msgLen)
rng.NextBytes(msg) // fill with rubbish
salt := make([]byte, 8)
rng.NextBytes(salt) // still more rubbish
digest := sha3.New256()
digest.Write(id)
digest.Write(seqBuf.Bytes())
digest.Write(msg)
digest.Write([]byte(salt))
hash := digest.Sum(nil)
// XXX This does not adhere to the rules: it has no Cmd field;
// since it has a payload it must be a Put, and so the id is
// also required and the Hash field should be a Sig instead, right?
var pkt = XLatticeMsg{
MsgN: &myMsgN,
Payload: msg,
Salt: salt,
Hash: hash,
}
// In each of these cases, the test proves that the field
// was present; otherwise the 'empty' value (zero, nil, etc)
// would have been returned.
msgNOut := pkt.GetMsgN()
c.Assert(msgNOut, Equals, myMsgN)
msgOut := pkt.GetPayload()
d.compareByteSlices(c, msgOut, msg)
saltOut := pkt.GetSalt()
d.compareByteSlices(c, saltOut, salt)
hashOut := pkt.GetHash()
d.compareByteSlices(c, hashOut, hash)
}
示例14: createKeyFromPassword
func createKeyFromPassword(pwd string) ([]byte, error) {
if len(pwd) < 8 {
return nil, fmt.Errorf("Password has to be at least 8 characters")
}
h := sha3.New256()
h.Write([]byte(pwd))
return h.Sum(nil), nil
}
示例15: SchnorrMComputeCollectiveChallenge
// (Either side) This function takes the aggregate public commitment
// r and returns sha3(m||r) for a given message.
func SchnorrMComputeCollectiveChallenge(suite abstract.Suite,
msg []byte,
pubCommit SchnorrMAggregateCommmitment) []byte {
p_bin, _ := pubCommit.P.MarshalBinary()
msg_and_p := append(msg, p_bin...)
hasher := sha3.New256()
hasher.Write(msg_and_p)
h := hasher.Sum(nil)
return h
}