Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. // crypto.go
  2. package crypto
  3.  
  4. import (
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "crypto/rand"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "log"
  14. )
  15.  
  16. func EncryptPassword(key []byte, text []byte) ([]byte, error) {
  17. block, err := aes.NewCipher(key)
  18. if err != nil {
  19. return nil, err
  20. }
  21.  
  22. b := base64.StdEncoding.EncodeToString(text)
  23. fmt.Printf("%s\n", b)
  24. cipherText := make([]byte, aes.BlockSize+len(b))
  25. iv := cipherText[:aes.BlockSize]
  26. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  27. return nil, err
  28. }
  29.  
  30. cfb := cipher.NewCFBEncrypter(block, iv)
  31. cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(b))
  32.  
  33. return cipherText, nil
  34. }
  35.  
  36. func DecryptPassword(key, text []byte) ([]byte, error) {
  37. block, err := aes.NewCipher(key)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if len(text) < aes.BlockSize {
  42. return nil, errors.New("ciphertext too short")
  43. }
  44.  
  45. iv := text[:aes.BlockSize]
  46. text = text[aes.BlockSize:]
  47. cfb := cipher.NewCFBDecrypter(block, iv)
  48. cfb.XORKeyStream(text, text)
  49. data, err := base64.StdEncoding.DecodeString(string(text))
  50. if err != nil {
  51. return nil, err
  52. }
  53. log.Println("DATA::", data)
  54. return data, nil
  55. }
  56.  
  57. // ComparePasswords - Decode, decrypt and compare passwords.
  58. func ComparePasswords(key []byte, password1 string, password2 string) bool {
  59. passDecoded1, err := hex.DecodeString(password1)
  60. if err != nil {
  61. log.Println(err)
  62. return false
  63. }
  64.  
  65. passDecoded2, err := hex.DecodeString(password2)
  66. if err != nil {
  67. log.Println(err)
  68. return false
  69. }
  70.  
  71. pass1, err := DecryptPassword(key, []byte(passDecoded1))
  72. if err != nil {
  73. log.Println(err)
  74. return false
  75. }
  76.  
  77. pass2, err := DecryptPassword(key, []byte(passDecoded2))
  78. if err != nil {
  79. log.Println(err)
  80. return false
  81. }
  82.  
  83. return string(pass1) == string(pass2)
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. // main.go
  94. package main
  95.  
  96. import (
  97. "context"
  98. "encoding/hex"
  99. "go-hybrid-alert/api/db"
  100. "go-hybrid-alert/api/schemas"
  101. "go-hybrid-alert/api/session"
  102. "go-hybrid-alert/util/crypto"
  103. "log"
  104. )
  105.  
  106. func main() {
  107.  
  108. userId := "danny@test.com"
  109. password := []byte("SECRET SAUCE!")
  110. key := session.GetPrivateKey()
  111. enc, err := crypto.EncryptPassword(key, password)
  112. if err != nil {
  113. log.Println("error encrypting: ", err)
  114. }
  115.  
  116. log.Println(string(enc))
  117.  
  118. dec, err := crypto.DecryptPassword(key, enc)
  119. if err != nil {
  120. log.Println("error decrypting: ", err)
  121. }
  122.  
  123. log.Println(dec)
  124.  
  125. // Add the new user/pass to firestore
  126. ctx := context.Background()
  127. user := schemas.FSUser{Password: string(enc)}
  128. if _, err := db.UsersRef.Doc(userId).Set(ctx, user); err != nil {
  129. log.Fatal(err)
  130. }
  131. }
  132.  
  133. // Output
  134. // U0VDUkVUIFNBVUNFIQ==
  135. // !��`0C��Z���Ta'�-�F�p?�y��7C}/E�#
  136. // DATA:: [83 69 67 82 69 84 32 83 65 85 67 69 33]
  137. // [83 69 67 82 69 84 32 83 65 85 67 69 33]
  138. // rpc error: code = Internal desc = grpc: error while marshaling: proto: field "google.firestore.v1.Value.ValueType" contains invalid UTF-8
  139. // exit status 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement