Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package main
  2. //smtp
  3.  
  4. import (
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "crypto/md5"
  8. "crypto/rand"
  9. "encoding/hex"
  10. "flag"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "net/smtp"
  15. "os"
  16. "strings"
  17. )
  18. var (
  19. to = flag.String("to", "michael.maksimov54@yandex.ru", "User name")
  20. sub = flag.String("sub", "test", "Subject")//заголовок
  21. mes = flag.String("mes", "hello", "message")
  22. )
  23.  
  24.  
  25. func main() {
  26. //fmt.Println("Starting the application...")
  27. //ciphertext := encrypt([]byte("B38mg520"), "password")
  28. //fmt.Printf("Encrypted: %x\n", ciphertext)
  29. //plaintext := decrypt(ciphertext, "password")
  30. //fmt.Printf("Decrypted: %s\n", plaintext)
  31. flag.Parse()
  32. path := "C:\\Users\\Миша\\go\\src\\Lab3.1\\data.txt"
  33. f, _ := ioutil.ReadFile(path)
  34. lines := strings.Split(string(f), "\n")
  35. for i:=0;i<len(lines);i++{
  36. fmt.Println(lines[i])
  37. }
  38. //test:=encrypt([]byte("B38mg520"), "password")
  39. //fmt.Println(test)
  40. //ioutil.WriteFile("pass",test,0644) //кодируем код от почты и записываем его в файл
  41. fmt.Println([]byte(lines[3]))
  42. fmt.Println(string (decrypt([]byte(lines[3]), "password")))
  43. from := lines[4]
  44. //to := "danila@posevin.com"
  45. //Обычно идентификатор(sidentity) должен быть пустой строкой, чтобы действовать как имя пользователя.
  46. auth := smtp.PlainAuth("",lines[2] ,string(decrypt([]byte(lines[3]), "password")), lines[0])
  47. message := `To: "` + *to + `"\r\nSubject: ` + *sub + `\r\n\r\n` + *mes+`\r\n`
  48.  
  49. if err := smtp.SendMail(lines[0]+":"+lines[1], auth, from, []string{*to}, []byte(message)); err != nil {
  50. fmt.Println("Error SendMail: ", err)
  51. os.Exit(1)
  52. }
  53. fmt.Println("Email Sent!")
  54. }
  55.  
  56.  
  57. func encrypt(data []byte, passphrase string) []byte {
  58. block, _ := aes.NewCipher([]byte(createHash(passphrase)))
  59. gcm, err := cipher.NewGCM(block)
  60. if err != nil {
  61. panic(err.Error())
  62. }
  63. nonce := make([]byte, gcm.NonceSize())
  64. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  65. panic(err.Error())
  66. }
  67. ciphertext := gcm.Seal(nonce, nonce, data, nil)
  68. return ciphertext
  69. }
  70.  
  71. func createHash(key string) string {
  72. hasher := md5.New()
  73. hasher.Write([]byte(key))
  74. return hex.EncodeToString(hasher.Sum(nil))
  75. }
  76.  
  77.  
  78. func decrypt(data []byte, passphrase string) []byte {
  79. key := []byte(createHash(passphrase))
  80. block, err := aes.NewCipher(key)
  81. if err != nil {
  82. panic(err.Error())
  83. }
  84. gcm, err := cipher.NewGCM(block)
  85. if err != nil {
  86. panic(err.Error())
  87. }
  88. nonceSize := gcm.NonceSize()
  89. nonce, ciphertext := data[:nonceSize], data[nonceSize:]
  90. plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
  91. if err != nil {
  92. panic(err.Error())
  93. }
  94. return plaintext
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement