crypto.go 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package crypto
  2. import (
  3. "crypto/sha256"
  4. "code.google.com/p/go.crypto/ripemd160"
  5. "github.com/ethereum/go-ethereum/ethutil"
  6. "github.com/obscuren/secp256k1-go"
  7. "github.com/obscuren/sha3"
  8. )
  9. // TODO refactor, remove (bin)
  10. func Sha3(data []byte) []byte {
  11. d := sha3.NewKeccak256()
  12. d.Write(data)
  13. return d.Sum(nil)
  14. }
  15. // Creates an ethereum address given the bytes and the nonce
  16. func CreateAddress(b []byte, nonce uint64) []byte {
  17. return Sha3(ethutil.NewValue([]interface{}{b, nonce}).Encode())[12:]
  18. }
  19. func Sha256(data []byte) []byte {
  20. hash := sha256.Sum256(data)
  21. return hash[:]
  22. }
  23. func Ripemd160(data []byte) []byte {
  24. ripemd := ripemd160.New()
  25. ripemd.Write(data)
  26. return ripemd.Sum(nil)
  27. }
  28. func Ecrecover(data []byte) []byte {
  29. var in = struct {
  30. hash []byte
  31. sig []byte
  32. }{data[:32], data[32:]}
  33. r, _ := secp256k1.RecoverPubkey(in.hash, in.sig)
  34. return r
  35. }