encrypt func added

This commit is contained in:
ed 2019-12-18 09:50:59 +01:00
parent 617b6cb03f
commit 3505619664
1 changed files with 32 additions and 6 deletions

38
main.go
View File

@ -1,20 +1,46 @@
package main package main
import ( import (
"crypto/aes"
"crypto/cipher"
"crypto/md5" "crypto/md5"
"crypto/rand"
"fmt" "fmt"
"io"
) )
func hashMkr(passwd string) []byte { func checkErr(err error) {
h := md5.New() if err != nil {
h.Write([]byte(passwd)) panic(err.Error())
return h.Sum(nil) }
}
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() { func main() {
var passwd string = "ThisAPassphrase" var passwd string = "ThisAPassphrase"
fmt.Println(passwd) fmt.Println(passwd)
hash := hashMkr(passwd) key := keyMkr(passwd)
fmt.Println(hash) fmt.Println(key)
data := []byte("Thi is some data")
dataSealed := encrypt(data,key)
fmt.Println(string(data))
fmt.Println(string(dataSealed))
} }