encrypt func added
This commit is contained in:
parent
617b6cb03f
commit
3505619664
38
main.go
38
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))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue