improve docs

This commit is contained in:
Dustin Stiles 2025-03-09 18:04:03 -04:00
parent ea027c6e4a
commit e674be0399
Signed by: duwstiles
GPG Key ID: BCD9912EC231FC87
3 changed files with 37 additions and 2 deletions

View File

@ -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

View 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
}
}

View File

@ -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)