OSDN Git Service

Merge pull request #37 from Bytom/dev
[bytom/vapor.git] / crypto / crypto.go
1 package crypto
2
3 import (
4         "github.com/vapor/common"
5         "golang.org/x/crypto/ripemd160"
6         "golang.org/x/crypto/sha3"
7 )
8
9 func DoubleSha256(b []byte) []byte {
10         hasher := sha3.New256()
11         hasher.Write(b)
12         sum := hasher.Sum(nil)
13         hasher.Reset()
14         hasher.Write(sum)
15         return hasher.Sum(nil)
16 }
17
18 func Sha256(data ...[]byte) []byte {
19         d := sha3.New256()
20         for _, b := range data {
21                 d.Write(b)
22         }
23         return d.Sum(nil)
24 }
25
26 func Sha256Hash(data ...[]byte) (h common.Hash) {
27         d := sha3.New256()
28         for _, b := range data {
29                 d.Write(b)
30         }
31         d.Sum(h[:0])
32         return h
33 }
34
35 func Sha3(data ...[]byte) []byte          { return Sha256(data...) }
36 func Sha3Hash(data ...[]byte) common.Hash { return Sha256Hash(data...) }
37
38 func Ripemd160(data []byte) []byte {
39         ripemd := ripemd160.New()
40         ripemd.Write(data)
41
42         return ripemd.Sum(nil)
43 }