diff --git a/main.go b/main.go index 9f82076..a56a494 100644 --- a/main.go +++ b/main.go @@ -1,20 +1,46 @@ package main import ( + "crypto/aes" + "crypto/cipher" "crypto/md5" + "crypto/rand" "fmt" + "io" ) -func hashMkr(passwd string) []byte { - h := md5.New() - h.Write([]byte(passwd)) - return h.Sum(nil) +func checkErr(err error) { + if err != nil { + panic(err.Error()) + } +} + +func keyMkr(passwd string) []byte { + key := md5.New() + key.Write([]byte(passwd)) + return key.Sum(nil) +} + +func encrypt(data []byte, key []byte) []byte { + block, err := aes.NewCipher(key) + checkErr(err) + gcm, err := cipher.NewGCM(block) + checkErr(err) + nonce := make([]byte, gcm.NonceSize()) + _ , err= io.ReadFull(rand.Reader, nonce) + checkErr(err) + sealed := gcm.Seal(nonce, nonce, data, nil) + return sealed } func main() { var passwd string = "ThisAPassphrase" fmt.Println(passwd) - hash := hashMkr(passwd) - fmt.Println(hash) + key := keyMkr(passwd) + fmt.Println(key) + data := []byte("Thi is some data") + dataSealed := encrypt(data,key) + fmt.Println(string(data)) + fmt.Println(string(dataSealed)) }