Move hashing and encryption to auth/helpers
Some checks are pending
/ test (push) Waiting to run

This commit is contained in:
Melody Becker 2025-03-31 23:06:27 +02:00
parent c269db5b02
commit c59b0c8340
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8
3 changed files with 110 additions and 95 deletions

View file

@ -1,65 +0,0 @@
package storage
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
)
// Copied and adjusted from: https://bruinsslot.jp/post/golang-crypto/
func Encrypt(key, data []byte) ([]byte, error) {
// key, salt, err := DeriveKey(key, nil)
// if err != nil {
// return nil, err
// }
//
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
// ciphertext = append(ciphertext, salt...)
return ciphertext, nil
}
func Decrypt(key, data []byte) ([]byte, error) {
// salt, data := data[len(data)-32:], data[:len(data)-32]
// key, _, err := DeriveKey(key, salt)
// if err != nil {
// return nil, err
// }
//
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce, ciphertext := data[:gcm.NonceSize()], data[gcm.NonceSize():]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}