From 1ed0e523773ef996693f6f5a7e616da6d4181ca2 Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sun, 14 Aug 2022 13:54:43 -0400 Subject: lib: add `TestRSAEncryptDecrypt` --- lib/rsa_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib') diff --git a/lib/rsa_test.go b/lib/rsa_test.go index f735f75..c0d4ce4 100644 --- a/lib/rsa_test.go +++ b/lib/rsa_test.go @@ -136,3 +136,41 @@ func TestRSAGenKey(t *testing.T) { return } } + +func TestRSAEncryptDecrypt(t *testing.T) { + pair, err := RSAGenKey() + if err != nil { + t.Errorf("genkey: %v", err) + return + } + pub := pair.Public + prv := pair.Private + + // [1] Encrypt. + msg := []byte("42") + enc := pub.Encrypt(msg) + if len(enc) < 1 { + t.Errorf("encrypt failed: %v", enc) + return + } + // [1] Decrypt. + dec := prv.Decrypt(enc) + if !BytesEqual(msg, dec) { + t.Errorf("decrypt failed: %v", dec) + return + } + + // [2] Encrypt. + msg = []byte("0xd1a4a6e870b40a261827f17741c19facf80d01a537d55e59abe5d615d961a23f") + enc = pub.Encrypt(msg) + if len(enc) < 1 { + t.Errorf("encrypt failed: %v", enc) + return + } + // [2] Decrypt. + dec = prv.Decrypt(enc) + if !BytesEqual(msg, dec) { + t.Errorf("decrypt failed: %v", dec) + return + } +} -- cgit v1.2.3