dev/command-saving (#1)
Reviewed-on: #1 Co-authored-by: jake <jake.young.dev@gmail.com> Co-committed-by: jake <jake.young.dev@gmail.com>
This commit is contained in:
49
cryptography/aes.go
Normal file
49
cryptography/aes.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cryptography
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func EncryptPassword(b []byte) ([]byte, error) {
|
||||
nonce := viper.Get("nonce").(string)
|
||||
dev := viper.Get("device").(string)
|
||||
|
||||
block, err := aes.NewCipher([]byte(dev))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aesg, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ct := aesg.Seal(nil, []byte(nonce), []byte(b), nil)
|
||||
return ct, nil
|
||||
}
|
||||
|
||||
func DecryptPassword(b []byte) (string, error) {
|
||||
nonce := viper.Get("nonce").(string)
|
||||
password := viper.Get("password").(string)
|
||||
dev := viper.Get("device").(string)
|
||||
|
||||
block, err := aes.NewCipher([]byte(dev))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
aesg, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
op, err := aesg.Open(nil, []byte(nonce), []byte(password), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(op), nil
|
||||
}
|
Reference in New Issue
Block a user