OSDN Git Service

fix conflicts
[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         "fmt"
21         //"io"
22         //"io/ioutil"
23         //"math/big"
24         //"os"
25
26         //"encoding/hex"
27         //"errors"
28
29         "github.com/bytom/common"
30         "golang.org/x/crypto/sha3"
31         "golang.org/x/crypto/ripemd160"
32 )
33
34
35 func Sha256(data ...[]byte) []byte {
36         d := sha3.New256()
37         for _, b := range data {
38                 d.Write(b)
39         }
40         return d.Sum(nil)
41 }
42
43 func Sha256Hash(data ...[]byte) (h common.Hash) {
44         d := sha3.New256()
45         for _, b := range data {
46                 d.Write(b)
47         }
48         d.Sum(h[:0])
49         return h
50 }
51
52 func Sha3(data ...[]byte) []byte          { return Sha256(data...) }
53 func Sha3Hash(data ...[]byte) common.Hash { return Sha256Hash(data...) }
54
55
56 func Ripemd160(data []byte) []byte {
57         ripemd := ripemd160.New()
58         ripemd.Write(data)
59
60         return ripemd.Sum(nil)
61 }
62
63 func PubkeyToAddress(pubBytes []byte) common.Address{
64         address, _ := common.AddressEncode("bm", 1, toInt(Ripemd160(Sha3(pubBytes))))
65         fmt.Printf(address)
66         return common.StringToAddress(address)
67 }
68
69 func AddressToPubkey(addr common.Address) (int, []byte, error) {
70         ver, data, err := common.AddressDecode("bm", addr.Str())
71         return ver, toBytes(data), err
72 }
73
74 func zeroBytes(bytes []byte) {
75         for i := range bytes {
76                 bytes[i] = 0
77         }
78 }
79
80 func toInt(bytes []byte) []int{
81         ints := make([]int, len(bytes))
82         for i := range bytes {
83                 ints[i] = int(bytes[i])
84         }
85         return ints
86 }
87
88 func toBytes(ints []int) []byte{
89         bytes := make([]byte, len(ints))
90         for i := range ints {
91                 bytes[i] = byte(ints[i])
92         }
93         return bytes
94 }