52 lines
2.1 KiB
Go
52 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"git.pspay.io/crypto-stories/custodial/pkg/crypto"
|
||
|
)
|
||
|
|
||
|
func TestKeyPair(t *testing.T) {
|
||
|
kp, _ := crypto.KeyPair{}.Random()
|
||
|
|
||
|
fmt.Println("\nPublic key base64: ")
|
||
|
fmt.Println(kp.PublicKeyBase64())
|
||
|
|
||
|
fmt.Println("\nPrivate key base64: ")
|
||
|
fmt.Println(kp.PrivateKeyBase64())
|
||
|
|
||
|
fmt.Println("\n Test encryption: ")
|
||
|
|
||
|
text := "Hello world!"
|
||
|
pubKey := crypto.RSAKey(kp.PublicKeyBase64())
|
||
|
encrypted := pubKey.EncryptToString(text)
|
||
|
|
||
|
fmt.Println(encrypted)
|
||
|
|
||
|
fmt.Println("\n Test decryption: ")
|
||
|
privKey := crypto.RSAKey(kp.PrivateKeyBase64())
|
||
|
decrypted := privKey.DecryptToString(encrypted)
|
||
|
|
||
|
fmt.Println(decrypted)
|
||
|
|
||
|
if text != decrypted {
|
||
|
t.Errorf("Decrypted text is not the same as original text")
|
||
|
t.Fail()
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestEnvCrypt(t *testing.T) {
|
||
|
fmt.Println("\n Test ENVCrypt (browser) encryption: ")
|
||
|
expected := "My secret text"
|
||
|
encrypted := "uJiPItNmQwRK7/zd6es7zmnyzR93ACp6tNaxGqUjabaCoD1X+KZFX7NE3GyGFVB4vdnwEXx+jsLyS3vXJN4ehR4r3f2J07q+SlEMVB5GxLH9gohulYzqwoQ5XW7zyZqqoD14QdX0fV1q6gZPhRiiMKmdE66+SC+ont/uDeXzr3wRnQMZf1dHQ9aAEjTqaTnGugPAbsDxRxxXBa/L44ZhNleV1wmKqTaEA0gFUtaikUjzS5yWj+9JQQi+EgzIVLGg+hVZ1ek3OASVbvlb4l7RyppJL6qxAjLyRhYOCT+I7R6v9uOAgiHGAhQF5c//FyrjiIHNiqVoFeG0FA9BiYaidw=="
|
||
|
privKey := crypto.RSAKey("UiZouRYYqk2Q8yOmmbyYLmn9J8YJ6C1A4JtN8wis0X8UlduvB7Xd4vCzrB7LXp9mq/zpo+CBZ41DzTJDtv/PNPWpmZInBwFeBFUfl+FPpWDldWptpuAAcqXD885MOs+0TqPhKtqWUUlkygftv+oZwv+YkxuMLvGZdipEEiwc2PIwV+F+beqo6MEd7Xwljhftp5YHkUy2x9SGgVlVWkVCUfkhqcgxXP9mLXz5h1rCoXDDhWr+gjKGfPQhmR7Lguon/fNIqNGN+ymG8ikO1A8if6ud4N8aOMQBNhirSUbb6KRZt4Dt8VgCOwcRpw4/NkEG7eUXiPmBQhsVjxkQ1dJG+QoKyNDMuALESVEjNAIAs2SQ3PBa65PFsqXQMq+kxnJQueSFCoZ0Lrh+4/iAJq01RqrlPSBsewHnIsN5bOTLg3FJluV/izyGdNdjwv6ue6UXkp2UisxovgjRBtbBZRDJcw+YGDecEhBSALkSuqBNE05VkQ6uiZQX23b2FhLtf7SrsgEPFwAOhwc/xRvTjvkLLiFSdd5i+5shQmE6KBfVngDl08Ctkobc7V/eo81tK+jsddh09JVjTOqN2S/nKjI7oASekNH1vfxRgEhd7CNJ33S2fd1eYGgsfLRzN9N6pW9e3tuXTpOSUJexXOWYaVJMUPMP20Da8/8awes4TIcJ980f9Q==")
|
||
|
decrypted := privKey.DecryptToString(encrypted)
|
||
|
fmt.Println(decrypted)
|
||
|
if decrypted != expected {
|
||
|
t.Errorf("Decrypted text is not the same as original text")
|
||
|
t.Fail()
|
||
|
}
|
||
|
}
|