mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 16:20:34 +00:00
improve docs
This commit is contained in:
parent
ea027c6e4a
commit
e674be0399
@ -12,6 +12,8 @@ import (
|
|||||||
|
|
||||||
// Encrypt uses AES-GCM to encrypt the given plaintext with the given key. The
|
// Encrypt uses AES-GCM to encrypt the given plaintext with the given key. The
|
||||||
// plaintext is sealed with a 12-byte nonce, which is prepended to the ciphertext.
|
// plaintext is sealed with a 12-byte nonce, which is prepended to the ciphertext.
|
||||||
|
// The nonce adds 28 bytes to the ciphertext, so the total length of the ciphertext
|
||||||
|
// is the length of the plaintext plus 28 bytes.
|
||||||
func Encrypt(key, plaintext []byte) ([]byte, error) {
|
func Encrypt(key, plaintext []byte) ([]byte, error) {
|
||||||
switch len(key) {
|
switch len(key) {
|
||||||
case 16, 24, 32: // AES-128, AES-192, AES-256
|
case 16, 24, 32: // AES-128, AES-192, AES-256
|
||||||
|
35
pkg/crypto/encryption/aead_test.go
Normal file
35
pkg/crypto/encryption/aead_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package encryption_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"koti.casa/numenor-labs/dsfx/pkg/crypto/encryption"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncryptDecrypt(t *testing.T) {
|
||||||
|
key := make([]byte, 32)
|
||||||
|
_, err := rand.Read(key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
plaintext := []byte("Hello, Worlskljfsjflskfjlskjfjslkfjsfjslkfjsfd!")
|
||||||
|
ciphertext, err := encryption.Encrypt(key, plaintext)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
decrypted, err := encryption.Decrypt(key, ciphertext)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(decrypted) != string(plaintext) {
|
||||||
|
t.Errorf("decrypted text does not match original plaintext")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,6 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"koti.casa/numenor-labs/dsfx/pkg/crypto/identity"
|
"koti.casa/numenor-labs/dsfx/pkg/crypto/identity"
|
||||||
@ -48,7 +47,6 @@ func TestImportExportPublic(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("keylen", len(exported))
|
|
||||||
imported, err := identity.ImportPublicKey(exported)
|
imported, err := identity.ImportPublicKey(exported)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to import key: %v", err)
|
t.Fatalf("failed to import key: %v", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user