Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- //smtp
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/md5"
- "crypto/rand"
- "encoding/hex"
- "flag"
- "fmt"
- "io"
- "io/ioutil"
- "net/smtp"
- "os"
- "strings"
- )
- var (
- to = flag.String("to", "michael.maksimov54@yandex.ru", "User name")
- sub = flag.String("sub", "test", "Subject")//заголовок
- mes = flag.String("mes", "hello", "message")
- )
- func main() {
- //fmt.Println("Starting the application...")
- //ciphertext := encrypt([]byte("B38mg520"), "password")
- //fmt.Printf("Encrypted: %x\n", ciphertext)
- //plaintext := decrypt(ciphertext, "password")
- //fmt.Printf("Decrypted: %s\n", plaintext)
- flag.Parse()
- path := "C:\\Users\\Миша\\go\\src\\Lab3.1\\data.txt"
- f, _ := ioutil.ReadFile(path)
- lines := strings.Split(string(f), "\n")
- for i:=0;i<len(lines);i++{
- fmt.Println(lines[i])
- }
- //test:=encrypt([]byte("B38mg520"), "password")
- //fmt.Println(test)
- //ioutil.WriteFile("pass",test,0644) //кодируем код от почты и записываем его в файл
- fmt.Println([]byte(lines[3]))
- fmt.Println(string (decrypt([]byte(lines[3]), "password")))
- from := lines[4]
- //to := "danila@posevin.com"
- //Обычно идентификатор(sidentity) должен быть пустой строкой, чтобы действовать как имя пользователя.
- auth := smtp.PlainAuth("",lines[2] ,string(decrypt([]byte(lines[3]), "password")), lines[0])
- message := `To: "` + *to + `"\r\nSubject: ` + *sub + `\r\n\r\n` + *mes+`\r\n`
- if err := smtp.SendMail(lines[0]+":"+lines[1], auth, from, []string{*to}, []byte(message)); err != nil {
- fmt.Println("Error SendMail: ", err)
- os.Exit(1)
- }
- fmt.Println("Email Sent!")
- }
- func encrypt(data []byte, passphrase string) []byte {
- block, _ := aes.NewCipher([]byte(createHash(passphrase)))
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- panic(err.Error())
- }
- nonce := make([]byte, gcm.NonceSize())
- if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
- panic(err.Error())
- }
- ciphertext := gcm.Seal(nonce, nonce, data, nil)
- return ciphertext
- }
- func createHash(key string) string {
- hasher := md5.New()
- hasher.Write([]byte(key))
- return hex.EncodeToString(hasher.Sum(nil))
- }
- func decrypt(data []byte, passphrase string) []byte {
- key := []byte(createHash(passphrase))
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err.Error())
- }
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- panic(err.Error())
- }
- nonceSize := gcm.NonceSize()
- nonce, ciphertext := data[:nonceSize], data[nonceSize:]
- plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
- if err != nil {
- panic(err.Error())
- }
- return plaintext
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement