本文整理汇总了Golang中github.com/dedis/crypto/abstract.Suite.Hash方法的典型用法代码示例。如果您正苦于以下问题:Golang Suite.Hash方法的具体用法?Golang Suite.Hash怎么用?Golang Suite.Hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/dedis/crypto/abstract.Suite
的用法示例。
在下文中一共展示了Suite.Hash方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewFile
func NewFile(suite abstract.Suite, path string) (*File, error) {
f, err := os.Open(path)
if err != nil {
log.Fatal("Failed opening file", path, err)
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
blocks := (fi.Size() + BlockSize - 1) / BlockSize
x := &File{
Name: path,
Hashes: make(map[string]int64, blocks),
}
for i := 0; int64(i) < blocks; i++ {
tmp := make([]byte, BlockSize)
_, err := f.Read(tmp)
if err != nil {
log.Fatal("Failed reading file", err)
}
h := suite.Hash()
h.Write(tmp)
x.Hashes[string(h.Sum(nil))] = int64((i * BlockSize))
}
return x, nil
}
示例2: verifyMessage
func verifyMessage(suite abstract.Suite, m interface{}, hash1 []byte) error {
// Make a copy of the signature
x := reflect.ValueOf(m).Elem().FieldByName("Sig")
sig := reflect.New(x.Type()).Elem()
sig.Set(x)
// Reset signature field
reflect.ValueOf(m).Elem().FieldByName("Sig").Set(reflect.ValueOf(crypto.SchnorrSig{})) // XXX: hack
// Marshal ...
mb, err := network.MarshalRegisteredType(m)
if err != nil {
return err
}
// ... and hash message
hash2, err := crypto.HashBytes(suite.Hash(), mb)
if err != nil {
return err
}
// Copy back original signature
reflect.ValueOf(m).Elem().FieldByName("Sig").Set(sig) // XXX: hack
// Compare hashes
if !bytes.Equal(hash1, hash2) {
return errors.New("Message has a different hash than the given one")
}
return nil
}
示例3: TestSuite
// Apply a standard set of validation tests to a ciphersuite.
func TestSuite(suite abstract.Suite) {
// Try hashing something
h := suite.Hash()
l := h.Size()
//println("HashLen: ",l)
h.Write([]byte("abc"))
hb := h.Sum(nil)
//println("Hash:")
//println(hex.Dump(hb))
if h.Size() != l || len(hb) != l {
panic("inconsistent hash output length")
}
// Generate some pseudorandom bits
s := suite.Cipher(hb)
sb := make([]byte, 128)
s.XORKeyStream(sb, sb)
//println("Stream:")
//println(hex.Dump(sb))
// Test if it generates two fresh keys with nil cipher
s1 := suite.NewKey(nil)
s2 := suite.NewKey(nil)
if s1.Equal(s2) {
panic("NewKey returns twice the same key given nil")
}
// Test if it creates the same with the same seed
st1 := suite.Cipher(hb)
st2 := suite.Cipher(hb)
s3 := suite.NewKey(st1)
s4 := suite.NewKey(st2)
if !s3.Equal(s4) {
panic("NewKey returns two different keys given same stream")
}
// Test if it creates two different with random stream
stream := random.Stream
s5 := suite.NewKey(stream)
s6 := suite.NewKey(stream)
if s5.Equal(s6) {
panic("NewKey returns same key given random stream")
}
// Test the public-key group arithmetic
TestGroup(suite)
}
示例4: TestSuite
// Apply a standard set of validation tests to a ciphersuite.
func TestSuite(suite abstract.Suite) {
// Try hashing something
h := suite.Hash()
l := h.Size()
//println("HashLen: ",l)
h.Write([]byte("abc"))
hb := h.Sum(nil)
//println("Hash:")
//println(hex.Dump(hb))
if h.Size() != l || len(hb) != l {
panic("inconsistent hash output length")
}
// Generate some pseudorandom bits
s := suite.Cipher(hb)
sb := make([]byte, 128)
s.XORKeyStream(sb, sb)
//println("Stream:")
//println(hex.Dump(sb))
// Test the public-key group arithmetic
TestGroup(suite)
}
示例5: HashFileSuite
// HashFileSuite returns the hash of a file using the hashing function of the
// suite given.
func HashFileSuite(suite abstract.Suite, file string) ([]byte, error) {
return HashFile(suite.Hash(), file)
}
示例6: HashStreamSuite
// HashStreamSuite will hash the stream using the hashing function of the suite
func HashStreamSuite(suite abstract.Suite, stream io.Reader) ([]byte, error) {
return HashStream(suite.Hash(), stream)
}
示例7: HashArgsSuite
// HashArgsSuite makes a new hash from the suite and calls HashArgs
func HashArgsSuite(suite abstract.Suite, args ...encoding.BinaryMarshaler) ([]byte, error) {
return HashArgs(suite.Hash(), args...)
}