forked from mirror/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
875 B
48 lines
875 B
10 years ago
|
package crypto
|
||
11 years ago
|
|
||
|
import (
|
||
10 years ago
|
"crypto/sha256"
|
||
|
|
||
|
"code.google.com/p/go.crypto/ripemd160"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethutil"
|
||
10 years ago
|
"github.com/obscuren/secp256k1-go"
|
||
10 years ago
|
"github.com/obscuren/sha3"
|
||
11 years ago
|
)
|
||
|
|
||
10 years ago
|
// TODO refactor, remove (bin)
|
||
10 years ago
|
func Sha3(data []byte) []byte {
|
||
11 years ago
|
d := sha3.NewKeccak256()
|
||
|
d.Write(data)
|
||
|
|
||
|
return d.Sum(nil)
|
||
|
}
|
||
11 years ago
|
|
||
|
// Creates an ethereum address given the bytes and the nonce
|
||
11 years ago
|
func CreateAddress(b []byte, nonce uint64) []byte {
|
||
10 years ago
|
return Sha3(ethutil.NewValue([]interface{}{b, nonce}).Encode())[12:]
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
func Sha256(data []byte) []byte {
|
||
|
hash := sha256.Sum256(data)
|
||
|
|
||
|
return hash[:]
|
||
|
}
|
||
|
|
||
|
func Ripemd160(data []byte) []byte {
|
||
|
ripemd := ripemd160.New()
|
||
|
ripemd.Write(data)
|
||
|
|
||
|
return ripemd.Sum(nil)
|
||
|
}
|
||
|
|
||
|
func Ecrecover(data []byte) []byte {
|
||
|
var in = struct {
|
||
|
hash []byte
|
||
|
sig []byte
|
||
|
}{data[:32], data[32:]}
|
||
|
|
||
|
r, _ := secp256k1.RecoverPubkey(in.hash, in.sig)
|
||
|
|
||
|
return r
|
||
|
}
|