add encryption and get page
This commit is contained in:
32
internal/crypto/cipher.go
Normal file
32
internal/crypto/cipher.go
Normal file
@ -0,0 +1,32 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
someDumbKey = []byte("9530e001b619e8e98a889055f06821bb")
|
||||
)
|
||||
|
||||
func Encrypt(plaintext string) (hexed string, err error) {
|
||||
aes, err := aes.NewCipher(someDumbKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("making new cipher: %w", err)
|
||||
}
|
||||
gcm, err := cipher.NewGCM(aes)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("making new gcm: %w", err)
|
||||
}
|
||||
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
_, err = rand.Read(nonce)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("generating nonce: %w", err)
|
||||
}
|
||||
outvalue := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
return hex.EncodeToString(outvalue), nil
|
||||
}
|
||||
10
internal/crypto/decrypt_off.go
Normal file
10
internal/crypto/decrypt_off.go
Normal file
@ -0,0 +1,10 @@
|
||||
//go:build !encon
|
||||
// +build !encon
|
||||
|
||||
package crypto
|
||||
|
||||
import "git.loyso.art/frx/eway/internal/entity"
|
||||
|
||||
func Decrypt(hexedcipher string) (plaintext string, err error) {
|
||||
return "", entity.SimpleError("this feature turned off")
|
||||
}
|
||||
35
internal/crypto/decrypt_on.go
Normal file
35
internal/crypto/decrypt_on.go
Normal file
@ -0,0 +1,35 @@
|
||||
//go:build encon
|
||||
// +build encon
|
||||
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Decrypt(hexedcipher string) (plaintext string, err error) {
|
||||
aes, err := aes.NewCipher(someDumbKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("making new cipher: %w", err)
|
||||
}
|
||||
gcm, err := cipher.NewGCM(aes)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("making new gcm: %w", err)
|
||||
}
|
||||
|
||||
nonceSize := gcm.NonceSize()
|
||||
valueDecoded, err := hex.DecodeString(hexedcipher)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decoding hexed value: %w", err)
|
||||
}
|
||||
nonce, cipherText := valueDecoded[:nonceSize], valueDecoded[nonceSize:]
|
||||
plaintextRaw, err := gcm.Open(nil, []byte(nonce), []byte(cipherText), nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("opening: %w", err)
|
||||
}
|
||||
|
||||
return string(plaintextRaw), nil
|
||||
}
|
||||
Reference in New Issue
Block a user