-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciphertext.go
More file actions
35 lines (29 loc) · 941 Bytes
/
ciphertext.go
File metadata and controls
35 lines (29 loc) · 941 Bytes
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
package bcl
import "encoding/base64"
type Ciphertext []byte
// CiphertextFromBytes casts a ciphertext from a byte slice
func CiphertextFromBytes(b []byte) (Ciphertext, error) {
if uint64(len(b)) > CryptoSecretBoxMessageBytesMax {
return nil, ErrBadPlaintextLength
}
return Ciphertext(b), nil
}
// CiphertextFromString casts a ciphertext from a string
func CiphertextFromString(s string) (Ciphertext, error) {
if uint64(len(s)) > CryptoSecretBoxMessageBytesMax {
return nil, ErrBadPlaintextLength
}
return Ciphertext(s), nil
}
// CiphertextFromBase64 casts a ciphertext from a base64 encoded string
func CiphertextFromBase64(arg string) (Ciphertext, error) {
b, err := base64.StdEncoding.DecodeString(arg)
if err != nil {
return nil, err
}
return CiphertextFromBytes(b)
}
// ToBase64 converts a ciphertext to a base64 encoded string
func (c Ciphertext) ToBase64() string {
return base64.StdEncoding.EncodeToString(c)
}