This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestSignVerify(t *testing.T) { | |
henk, _ := NewRsaIdentity() | |
// A public message from Henk. | |
// note that the message is a byte array, not just a string. | |
msg := []byte("Wilders doet tenminste iets tegen de politiek.") | |
// Henk signs the message with his private key. This will show the recipient | |
// proof that this message is indeed from Henk | |
sig, err := henk.Sign(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sign returns a signature made by combining the message and the signers private key | |
// With the r.Verify function, the signature can be checked. | |
func (r *RsaIdentity) Sign(msg []byte) (signature []byte, err error) { | |
hs := r.getHashSum(msg) | |
signature, err := rsa.SignPKCS1v15(rand.Reader, r.private, crypto.SHA256, hs) | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestEncryptDecrypt(t *testing.T) { | |
henk, _ := NewRsaIdentity() | |
ingrid, _ := NewRsaIdentity() | |
// a message from Henk to Ingrid | |
msg := []byte("Die uitkeringstrekkers pikken al onze banen in.") | |
// Lets encrypt it, we want to sent it to Ingrid, thus, we use her public key. | |
encryptedMessage, err := henk.Encrypt(msg, ingrid.public) | |
if err != nil { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestPublicKeyExample(t *testing.T) { | |
henk, _ := NewRsaIdentity() | |
ingrid, _ := NewRsaIdentity() | |
msg := []byte("Negenennegentig procent van de problemen in de wereld worden veroorzaakt door buitenlanders.") | |
// Lets encrypt it, we want to sent it to Ingrid, thus, we use her public key. | |
encryptedMessage, _ := henk.Encrypt(msg, ingrid.public) | |
fmt.Printf("%s\n", msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Decrypt a message using your private key. | |
// A received message should be encrypted using the receivers public key. | |
func (r *RsaIdentity) Decrypt(msg []byte) ([]byte, error) { | |
label := []byte("") | |
hash := sha256.New() | |
return rsa.DecryptOAEP(hash, rand.Reader, r.private, msg, label) | |
} |