Ethereum_CLI.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "crypto/aes"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "github.com/ethereum/go-ethereum/common/hexutil"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. )
  11. func main() {
  12. privateKey, err := crypto.GenerateKey()
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. privateKeyBytes := crypto.FromECDSA(privateKey)
  17. password := hexutil.Encode(privateKeyBytes)[2:]
  18. fmt.Println(hexutil.Encode(privateKeyBytes)[2:])
  19. hashSum := GenHash(password)
  20. encHashSum := Encrypt(hashSum)
  21. fmt.Println(encHashSum)
  22. }
  23. const keyAES = "keyAES!"
  24. func GenHash(password string) string {
  25. hash := sha256.New()
  26. _, err := hash.Write([]byte(password))
  27. if err != nil {
  28. return ""
  29. }
  30. return fmt.Sprintf("%x", hash.Sum(nil))
  31. }
  32. func Encrypt(hash string) string {
  33. key := []byte(keyAES)
  34. block, err := aes.NewCipher(key)
  35. if err != nil {
  36. return ""
  37. }
  38. out := make([]byte, len(hash))
  39. block.Encrypt(out, []byte(hash))
  40. return hex.EncodeToString(out)
  41. }
  42. func Decrypt(cipherText string) (string, error) {
  43. text, err := hex.DecodeString(cipherText)
  44. if err != nil {
  45. return "", err
  46. }
  47. key := []byte(keyAES)
  48. block, err := aes.NewCipher(key)
  49. if err != nil {
  50. return "", err
  51. }
  52. pt := make([]byte, len(text))
  53. block.Decrypt(pt, text)
  54. s := string(pt[:])
  55. return s, nil
  56. }