-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcryptor_aes.go
72 lines (66 loc) · 2.02 KB
/
cryptor_aes.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
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package cryptfs
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"fmt"
"io"
)
type AESCryptor struct {
cphr cipher.Block
}
// NewAESCryptor returns an Cryptor which performs AES encryption/decryption.
//
// The key must be 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func NewAESCryptor(key []byte) (*AESCryptor, error) {
cphr, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return &AESCryptor{cphr: cphr}, nil
}
func (c *AESCryptor) encrypt(data []byte) ([]byte, error) {
gcm, err := cipher.NewGCM(c.cphr)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
out := gcm.Seal(nonce, nonce, data, nil)
return out, nil
}
func (c *AESCryptor) decrypt(ciphertext []byte) ([]byte, error) {
gcm, err := cipher.NewGCM(c.cphr)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("nonce is too small")
}
nonce, encryptedMessage := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, encryptedMessage, nil)
if err != nil {
return nil, fmt.Errorf("AES decryption failed: %w", err)
}
return plaintext, nil
}