2023-09-19 00:22:50 +00:00
|
|
|
// Package encryption provides functions to encode/decode AES encrypted secrets.
|
|
|
|
//
|
|
|
|
// Encryption is used to store sensitive information in the database, such as 2FA TOTP secrets
|
|
|
|
// for users who have 2FA authentication enabled.
|
|
|
|
//
|
|
|
|
// For new key generation, see pkg/config/variable.go#NewAESKey.
|
|
|
|
package encryption
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2024-05-30 06:20:24 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/encryption/keygen"
|
2023-09-19 00:22:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Encrypt a byte stream using the site's AES passphrase.
|
|
|
|
func Encrypt(input []byte) ([]byte, error) {
|
|
|
|
if len(config.Current.Encryption.AESKey) == 0 {
|
|
|
|
return nil, errors.New("AES key not configured")
|
|
|
|
}
|
|
|
|
|
2024-05-30 06:20:24 +00:00
|
|
|
return keygen.EncryptWithAESKey(input, config.Current.Encryption.AESKey)
|
2023-09-19 00:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptString encrypts a string value and returns the cipher text.
|
|
|
|
func EncryptString(input string) ([]byte, error) {
|
|
|
|
return Encrypt([]byte(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt a byte stream using the site's AES passphrase.
|
|
|
|
func Decrypt(data []byte) ([]byte, error) {
|
|
|
|
if len(config.Current.Encryption.AESKey) == 0 {
|
|
|
|
return nil, errors.New("AES key not configured")
|
|
|
|
}
|
|
|
|
|
2024-05-30 06:20:24 +00:00
|
|
|
return keygen.DecryptWithAESKey(data, config.Current.Encryption.AESKey)
|
2023-09-19 00:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptString decrypts a string value from ciphertext.
|
|
|
|
func DecryptString(data []byte) (string, error) {
|
|
|
|
decoded, err := Decrypt(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(decoded), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash a byte array as SHA256 and returns the hex string.
|
|
|
|
func Hash(input []byte) string {
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(input)
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyHash hashes a byte array and checks the result.
|
|
|
|
func VerifyHash(input []byte, expect string) bool {
|
|
|
|
return Hash(input) == expect
|
|
|
|
}
|