mirror of
https://github.com/vwxyzjn/portwarden
synced 2025-12-16 07:53:14 +00:00
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
// modified from
|
|
// https://www.thepolyglotdeveloper.com/2018/02/encrypt-decrypt-data-golang-application-crypto-packages/
|
|
package portwarden
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
|
)
|
|
|
|
const (
|
|
Salt = `,(@0vd<)D6c3:5jI;4BZ(#Gx2IZ6B>`
|
|
)
|
|
|
|
// derive a key from the master password
|
|
func deriveKey(passphrase string) []byte {
|
|
return pbkdf2.Key([]byte(passphrase), []byte(Salt), 4096, 32, sha256.New)
|
|
}
|
|
|
|
func encrypt(data []byte, passphrase string) ([]byte, error) {
|
|
block, _ := aes.NewCipher(deriveKey(passphrase))
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return []byte{}, err
|
|
}
|
|
ciphertext := gcm.Seal(nonce, nonce, data, nil)
|
|
return ciphertext, nil
|
|
}
|
|
|
|
func decrypt(data []byte, passphrase string) ([]byte, error) {
|
|
key := deriveKey(passphrase)
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
nonceSize := gcm.NonceSize()
|
|
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
|
|
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
return plaintext, nil
|
|
}
|
|
|
|
func EncryptFile(filename string, data []byte, passphrase string) error {
|
|
f, _ := os.Create(filename)
|
|
defer f.Close()
|
|
ciphertext, err := encrypt(data, passphrase)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f.Write(ciphertext)
|
|
return nil
|
|
}
|
|
|
|
func DecryptFile(filename string, passphrase string) ([]byte, error) {
|
|
data, _ := ioutil.ReadFile(filename)
|
|
return decrypt(data, passphrase)
|
|
}
|