Skip to content

Commit 809a282

Browse files
committed
add more examples
1 parent c25ecad commit 809a282

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

README.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ openssl rsa -in private.pem -out public.pem -pubout -outform PEM
1818
package main
1919

2020
import (
21+
"fmt"
2122
"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
22-
"log"
2323
)
2424

2525
const (
@@ -62,26 +62,85 @@ bQIDAQAB
6262
-----END PUBLIC KEY-----
6363
`
6464
)
65+
```
66+
```go
67+
package main
68+
69+
import (
70+
"log"
71+
"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
72+
)
73+
74+
func rsaEncryptDecrypt() {
75+
// example_1: Encrypt/Decrypt with RSA
76+
expectedText := "some expectedText goes here"
77+
encoder := cryptolib.NewEncoder(testPublicKey)
78+
rsaEncryptedText := encoder.Encrypt(expectedText)
79+
80+
decoder := cryptolib.NewDecoder(testPrivateKey)
81+
rsaDecryptedText := decoder.Decrypt(rsaEncryptedText)
82+
83+
if expectedText != rsaDecryptedText {
84+
log.Println("failed to decrypt")
85+
}
86+
}
87+
88+
```
89+
90+
```go
91+
package main
92+
93+
import (
94+
"log"
95+
"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
96+
)
6597

66-
func main() {
98+
func rsaEncryptDecryptAndBase85Encode() {
6799
text := "some text data"
68100

69101
// encoding example
70102
encoder := cryptolib.NewEncoder(testPublicKey)
71103
rsaEncodedText := encoder.Encrypt(text)
72104
base85EncodedText := encoder.ToBase85(rsaEncodedText)
73-
log.Printf(base85EncodedText)
105+
log.Println(base85EncodedText)
74106

75107
// decoding example
76108
decoder := cryptolib.NewDecoder(testPrivateKey)
77109
base85decodedText := decoder.FromBase85(base85EncodedText)
78110
rsaDecodedText := decoder.Decrypt(base85decodedText)
79-
log.Printf(rsaDecodedText) // -> "some text data"
111+
log.Println(rsaDecodedText) // -> "some text data"
80112
if rsaDecodedText != text {
81113
log.Fatal("failed to decrypt")
82114
}
83115
}
116+
```
117+
118+
```go
119+
package main
120+
121+
import (
122+
"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
123+
"log"
124+
)
84125

126+
func rsaEncryptDecryptAndBase64Encode() {
127+
text := "some text data"
128+
129+
// encoding example
130+
encoder := cryptolib.NewEncoder(testPublicKey)
131+
rsaEncodedText := encoder.Encrypt(text)
132+
base64EncodedText := encoder.ToBase64(rsaEncodedText)
133+
log.Println(base64EncodedText)
134+
135+
// decoding example
136+
decoder := cryptolib.NewDecoder(testPrivateKey)
137+
base64decodedText := decoder.FromBase64(base64EncodedText)
138+
rsaDecodedText := decoder.Decrypt(base64decodedText)
139+
log.Println(rsaDecodedText) // -> "some text data"
140+
if rsaDecodedText != text {
141+
log.Fatalln("failed to decrypt")
142+
}
143+
}
85144
```
86145

87146
**NOTE**: please refer to [rsa-encrypt-decrypt-python](https://github.com/vspaz/rsa-encrypt-decrypt-python) if you need the Python lib.

0 commit comments

Comments
 (0)