forked from kleopatra999/go-encrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
73 lines (61 loc) · 2.13 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Package encrypt provides simplified utilities to encrypt and decrypt data using standard libraries.
*/
package encrypt // import "github.com/ryanskidmore/encrypt-go"
import "encoding/base64"
const (
// AES128Bits is the number of bits in an AES-128 key.
AES128Bits = 128
// AES192Bits is the number of bits in an AES-192 key.
AES192Bits = 192
// AES256Bits is the number of bits in an AES-256 key.
AES256Bits = 256
// RSA2048Bits is the number of bits in a 2048-bit RSA key.
RSA2048Bits = 2048
// 3072 Bit Key
RSA3072Bits = 3072
)
// Encryptor encrypts data.
type Encryptor interface {
// Encrypt encrypts the given data. The returned data will be base64 encoded.
Encrypt([]byte) ([]byte, error)
}
// Decryptor decrypts data.
type Decryptor interface {
// Decrypt decrypts base64 encoded data.
Decrypt([]byte) ([]byte, error)
}
// Transformer is both an Encryptor and Decryptor.
type Transformer interface {
Encryptor
Decryptor
}
// GenerateAESKey generates a new AES key for the given number of bits.
// The returned key will be base64 encoded.
func GenerateAESKey(bits int) (string, error) {
return generateAESKey(bits)
}
// NewAESTransformer creates a new AES Transformer with the given base64 encoded key.
func NewAESTransformer(key string) (Transformer, error) {
return newAESTransformer(key)
}
// GenerateRSAKeys generates a new public and private (in that order of returned values) key.
func GenerateRSAKeys(bits int) (string, string, error) {
return generateRSAKeys(bits)
}
// NewRSAEncryptor returns a new RSA Encryptor with the given public key.
func NewRSAEncryptor(publicKey string) (Encryptor, error) {
return newRSAEncryptor(publicKey)
}
// NewRSATransformer returns a new RSA Transformer with the given private key.
func NewRSATransformer(privateKey string) (Transformer, error) {
return newRSATransformer(privateKey)
}
// EncodeToString is a simple wrapper for base64 encoding.
func EncodeToString(value []byte) string {
return base64.StdEncoding.EncodeToString(value)
}
// DecodeString is a simple wrapper for base64 decoding.
func DecodeString(value string) ([]byte, error) {
return base64.StdEncoding.DecodeString(value)
}