crypto.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package crypto
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "code.google.com/p/go.crypto/ripemd160"
  8. "github.com/ethereum/go-ethereum/crypto/secp256k1"
  9. "github.com/ethereum/go-ethereum/crypto/sha3"
  10. "github.com/ethereum/go-ethereum/ethutil"
  11. "github.com/obscuren/ecies"
  12. )
  13. func init() {
  14. // specify the params for the s256 curve
  15. ecies.AddParamsForCurve(S256(), ecies.ECIES_AES128_SHA256)
  16. }
  17. func Sha3(data []byte) []byte {
  18. d := sha3.NewKeccak256()
  19. d.Write(data)
  20. return d.Sum(nil)
  21. }
  22. // Creates an ethereum address given the bytes and the nonce
  23. func CreateAddress(b []byte, nonce uint64) []byte {
  24. return Sha3(ethutil.NewValue([]interface{}{b, nonce}).Encode())[12:]
  25. }
  26. func Sha256(data []byte) []byte {
  27. hash := sha256.Sum256(data)
  28. return hash[:]
  29. }
  30. func Ripemd160(data []byte) []byte {
  31. ripemd := ripemd160.New()
  32. ripemd.Write(data)
  33. return ripemd.Sum(nil)
  34. }
  35. func Ecrecover(data []byte) []byte {
  36. var in = struct {
  37. hash []byte
  38. sig []byte
  39. }{data[:32], data[32:]}
  40. r, _ := secp256k1.RecoverPubkey(in.hash, in.sig)
  41. return r
  42. }
  43. // New methods using proper ecdsa keys from the stdlib
  44. func ToECDSA(prv []byte) *ecdsa.PrivateKey {
  45. if len(prv) == 0 {
  46. return nil
  47. }
  48. priv := new(ecdsa.PrivateKey)
  49. priv.PublicKey.Curve = S256()
  50. priv.D = ethutil.BigD(prv)
  51. priv.PublicKey.X, priv.PublicKey.Y = S256().ScalarBaseMult(prv)
  52. return priv
  53. }
  54. func FromECDSA(prv *ecdsa.PrivateKey) []byte {
  55. if prv == nil {
  56. return nil
  57. }
  58. return prv.D.Bytes()
  59. }
  60. func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
  61. if len(pub) == 0 {
  62. return nil
  63. }
  64. x, y := elliptic.Unmarshal(S256(), pub)
  65. return &ecdsa.PublicKey{S256(), x, y}
  66. }
  67. func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
  68. if pub == nil {
  69. return nil
  70. }
  71. return elliptic.Marshal(S256(), pub.X, pub.Y)
  72. }
  73. func GenerateKey() (*ecdsa.PrivateKey, error) {
  74. return ecdsa.GenerateKey(S256(), rand.Reader)
  75. }
  76. func SigToPub(hash, sig []byte) *ecdsa.PublicKey {
  77. s := Ecrecover(append(hash, sig...))
  78. x, y := elliptic.Unmarshal(S256(), s)
  79. return &ecdsa.PublicKey{S256(), x, y}
  80. }
  81. func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
  82. sig, err = secp256k1.Sign(hash, prv.D.Bytes())
  83. return
  84. }
  85. func Encrypt(pub *ecdsa.PublicKey, message []byte) ([]byte, error) {
  86. return ecies.Encrypt(rand.Reader, ecies.ImportECDSAPublic(pub), message, nil, nil)
  87. }
  88. func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) {
  89. key := ecies.ImportECDSA(prv)
  90. return key.Decrypt(rand.Reader, ct, nil, nil)
  91. }