OSDN Git Service

add test script
[bytom/bytom.git] / crypto / crypto.go
1 // Copyright 2014 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
3 //
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17 package crypto
18
19 import (
20         "github.com/bytom/common"
21         "golang.org/x/crypto/ripemd160"
22         "golang.org/x/crypto/sha3"
23 )
24
25 func DoubleSha256(b []byte) []byte {
26         hasher := sha3.New256()
27         hasher.Write(b)
28         sum := hasher.Sum(nil)
29         hasher.Reset()
30         hasher.Write(sum)
31         return hasher.Sum(nil)
32 }
33
34 func Sha256(data ...[]byte) []byte {
35         d := sha3.New256()
36         for _, b := range data {
37                 d.Write(b)
38         }
39         return d.Sum(nil)
40 }
41
42 func Sha256Hash(data ...[]byte) (h common.Hash) {
43         d := sha3.New256()
44         for _, b := range data {
45                 d.Write(b)
46         }
47         d.Sum(h[:0])
48         return h
49 }
50
51 func Sha3(data ...[]byte) []byte          { return Sha256(data...) }
52 func Sha3Hash(data ...[]byte) common.Hash { return Sha256Hash(data...) }
53
54 func Ripemd160(data []byte) []byte {
55         ripemd := ripemd160.New()
56         ripemd.Write(data)
57
58         return ripemd.Sum(nil)
59 }