@@ -23,13 +23,14 @@ import (
2323 "crypto/rand"
2424 "crypto/rsa"
2525 "fmt"
26+ "hash"
27+ "io"
28+ "os"
29+
2630 "github.com/goph/emperror"
2731 "github.com/pkg/errors"
2832 "golang.org/x/crypto/blake2b"
2933 "golang.org/x/crypto/nacl/box"
30- "hash"
31- "io"
32- "os"
3334)
3435
3536func init () {
@@ -84,40 +85,48 @@ func GeneratePrivateKey(size int) *rsa.PrivateKey {
8485 return privateKey
8586}
8687
88+ // DefaultCipherEncrypter returns a NOOP encrypter.
8789func DefaultCipherEncrypter () Encrypt {
8890 return & NOOP {}
8991}
9092
93+ // DEfaultCipherDecrypter returns a NOOP decrypter.
9194func DefaultCipherDecrypter () Decrypt {
9295 return & NOOP {}
9396}
9497
9598// NOOP will just return the message
9699type NOOP struct {}
97100
101+ // GetAlgorithm returns None.
98102func (* NOOP ) GetAlgorithm () AlgorithmType {
99103 return None
100104}
101105
106+ // GetKID returns none.
102107func (* NOOP ) GetKID () string {
103108 return "none"
104109}
105110
111+ //EncryptMessage simply returns the message given.
106112func (* NOOP ) EncryptMessage (message []byte ) (crypt []byte , nonce []byte , err error ) {
107113 return message , []byte {}, nil
108114}
109115
116+ // DecryptMessage simply returns the message given.
110117func (* NOOP ) DecryptMessage (cipher []byte , nonce []byte ) (message []byte , err error ) {
111118 return cipher , nil
112119}
113120
121+ // GetAlgorithm returns the algorithm type.
114122func (c * rsaEncrypterDecrypter ) GetAlgorithm () AlgorithmType {
115123 if c .recipientPublicKey == nil || c .senderPublicKey == nil {
116124 return RSASymmetric
117125 }
118126 return RSAAsymmetric
119127}
120128
129+ // GetKID returns the KID.
121130func (c * rsaEncrypterDecrypter ) GetKID () string {
122131 return c .kid
123132}
@@ -132,6 +141,7 @@ type rsaEncrypterDecrypter struct {
132141 label []byte
133142}
134143
144+ // NewRSAEncrypter returns an RSA encrypter.
135145func NewRSAEncrypter (hash crypto.Hash , senderPrivateKey * rsa.PrivateKey , recipientPublicKey * rsa.PublicKey , kid string ) Encrypt {
136146 return & rsaEncrypterDecrypter {
137147 kid : kid ,
@@ -142,6 +152,7 @@ func NewRSAEncrypter(hash crypto.Hash, senderPrivateKey *rsa.PrivateKey, recipie
142152 }
143153}
144154
155+ // NewRSADecrypter returns an RSA decrypter.
145156func NewRSADecrypter (hash crypto.Hash , recipientPrivateKey * rsa.PrivateKey , senderPublicKey * rsa.PublicKey , kid string ) Decrypt {
146157 return & rsaEncrypterDecrypter {
147158 kid : kid ,
@@ -152,6 +163,7 @@ func NewRSADecrypter(hash crypto.Hash, recipientPrivateKey *rsa.PrivateKey, send
152163 }
153164}
154165
166+ // EncryptMessage encrypts the message using RSA.
155167func (c * rsaEncrypterDecrypter ) EncryptMessage (message []byte ) ([]byte , []byte , error ) {
156168 cipherdata , err := rsa .EncryptOAEP (
157169 c .hasher .New (),
@@ -183,6 +195,7 @@ func (c *rsaEncrypterDecrypter) EncryptMessage(message []byte) ([]byte, []byte,
183195 return cipherdata , signature , nil
184196}
185197
198+ // DecryptMessage decrypts the message using RSA.
186199func (c * rsaEncrypterDecrypter ) DecryptMessage (cipher []byte , nonce []byte ) ([]byte , error ) {
187200 decrypted , err := rsa .DecryptOAEP (
188201 c .hasher .New (),
@@ -219,14 +232,17 @@ type encryptBox struct {
219232 sharedEncryptKey * [32 ]byte
220233}
221234
235+ // GetAlgorithm returns the algorithm type.
222236func (enBox * encryptBox ) GetAlgorithm () AlgorithmType {
223237 return Box
224238}
225239
240+ // GetKID returns the KID.
226241func (enBox * encryptBox ) GetKID () string {
227242 return enBox .kid
228243}
229244
245+ // NewBoxEncrypter returns a new box encrypter.
230246func NewBoxEncrypter (senderPrivateKey [32 ]byte , recipientPublicKey [32 ]byte , kid string ) Encrypt {
231247
232248 encrypter := encryptBox {
@@ -241,6 +257,7 @@ func NewBoxEncrypter(senderPrivateKey [32]byte, recipientPublicKey [32]byte, kid
241257 return & encrypter
242258}
243259
260+ // Encrypt message encrypts the message using the box algorithm.
244261func (enBox * encryptBox ) EncryptMessage (message []byte ) ([]byte , []byte , error ) {
245262 var nonce [24 ]byte
246263 if _ , err := io .ReadFull (rand .Reader , nonce [:]); err != nil {
@@ -259,14 +276,17 @@ type decryptBox struct {
259276 sharedDecryptKey * [32 ]byte
260277}
261278
279+ // GetAlgorithm returns the algorithm type.
262280func (deBox * decryptBox ) GetAlgorithm () AlgorithmType {
263281 return Box
264282}
265283
284+ // GetKID returns the KID.
266285func (deBox * decryptBox ) GetKID () string {
267286 return deBox .kid
268287}
269288
289+ // NewBoxDecrypter returns a new box decrypter.
270290func NewBoxDecrypter (recipientPrivateKey [32 ]byte , senderPublicKey [32 ]byte , kid string ) Decrypt {
271291
272292 decrypter := decryptBox {
@@ -281,6 +301,7 @@ func NewBoxDecrypter(recipientPrivateKey [32]byte, senderPublicKey [32]byte, kid
281301 return & decrypter
282302}
283303
304+ // DecryptMessage decrypts the message using the box algorithm.
284305func (deBox * decryptBox ) DecryptMessage (cipher []byte , nonce []byte ) ([]byte , error ) {
285306 var decryptNonce [24 ]byte
286307 copy (decryptNonce [:], nonce [:24 ])
0 commit comments