Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package ciphers
  2.  
  3. import (
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/sha512"
  7. "crypto/x509"
  8. "encoding/pem"
  9. "log"
  10. )
  11.  
  12. // GenerateKeyPair generates a new key pair
  13. func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
  14. privkey, err := rsa.GenerateKey(rand.Reader, bits)
  15. if err != nil {
  16. log.Error(err)
  17. }
  18. return privkey, &privkey.PublicKey
  19. }
  20.  
  21. // PrivateKeyToBytes private key to bytes
  22. func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
  23. privBytes := pem.EncodeToMemory(
  24. &pem.Block{
  25. Type: "RSA PRIVATE KEY",
  26. Bytes: x509.MarshalPKCS1PrivateKey(priv),
  27. },
  28. )
  29.  
  30. return privBytes
  31. }
  32.  
  33. // PublicKeyToBytes public key to bytes
  34. func PublicKeyToBytes(pub *rsa.PublicKey) []byte {
  35. pubASN1, err := x509.MarshalPKIXPublicKey(pub)
  36. if err != nil {
  37. log.Error(err)
  38. }
  39.  
  40. pubBytes := pem.EncodeToMemory(&pem.Block{
  41. Type: "RSA PUBLIC KEY",
  42. Bytes: pubASN1,
  43. })
  44.  
  45. return pubBytes
  46. }
  47.  
  48. // BytesToPrivateKey bytes to private key
  49. func BytesToPrivateKey(priv []byte) *rsa.PrivateKey {
  50. block, _ := pem.Decode(priv)
  51. enc := x509.IsEncryptedPEMBlock(block)
  52. b := block.Bytes
  53. var err error
  54. if enc {
  55. log.Println("is encrypted pem block")
  56. b, err = x509.DecryptPEMBlock(block, nil)
  57. if err != nil {
  58. log.Error(err)
  59. }
  60. }
  61. key, err := x509.ParsePKCS1PrivateKey(b)
  62. if err != nil {
  63. log.Error(err)
  64. }
  65. return key
  66. }
  67.  
  68. // BytesToPublicKey bytes to public key
  69. func BytesToPublicKey(pub []byte) *rsa.PublicKey {
  70. block, _ := pem.Decode(pub)
  71. enc := x509.IsEncryptedPEMBlock(block)
  72. b := block.Bytes
  73. var err error
  74. if enc {
  75. log.Println("is encrypted pem block")
  76. b, err = x509.DecryptPEMBlock(block, nil)
  77. if err != nil {
  78. log.Error(err)
  79. }
  80. }
  81. ifc, err := x509.ParsePKIXPublicKey(b)
  82. if err != nil {
  83. log.Error(err)
  84. }
  85. key, ok := ifc.(*rsa.PublicKey)
  86. if !ok {
  87. log.Error("not ok")
  88. }
  89. return key
  90. }
  91.  
  92. // EncryptWithPublicKey encrypts data with public key
  93. func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) []byte {
  94. hash := sha512.New()
  95. ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, pub, msg, nil)
  96. if err != nil {
  97. log.Error(err)
  98. }
  99. return ciphertext
  100. }
  101.  
  102. // DecryptWithPrivateKey decrypts data with private key
  103. func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) []byte {
  104. hash := sha512.New()
  105. plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil)
  106. if err != nil {
  107. log.Error(err)
  108. }
  109. return plaintext
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement