Repository navigation
Expand file tree
/
Copy pathindex.go
More file actions
123 lines (101 loc) · 2.68 KB
/
Copy pathindex.go
File metadata and controls
123 lines (101 loc) · 2.68 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package aesf
import (
"bytes"
"errors"
"io"
"strconv"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha256"
"github.com/dchest/uniuri"
"golang.org/x/crypto/pbkdf2"
)
const (
// Default iterations of derivation key producing
DefaultIterations = 4096
// Authentication field size
SignatureKeySize = 10
// Password verifier size
PwdVerifierSize = 2
)
var (
// Invalid decrypt key
ErrBadPassword = errors.New("asef: invalid password")
// Envalid data signature
ErrSignatureFail = errors.New("asef: invalid signature key")
)
// Encrypt key size should be between 8 bytes to 64 bytes.
type PasswordSizeError int
func (p PasswordSizeError) Error() string {
return "asef: invalid key size " + strconv.Itoa(int(p))
}
func calSaltSize(pwdSize int) (int, error) {
switch {
case pwdSize >= 8 && pwdSize < 32:
return 8, nil
case pwdSize >= 32 && pwdSize < 48:
return 12, nil
case pwdSize >= 48 && pwdSize < 64:
return 16, nil
}
return 0, PasswordSizeError(pwdSize)
}
type AESf struct {
password string
iter, saltSize, keySize int
}
func New(password string) (*AESf, error) {
saltSize, err := calSaltSize(len(password))
if err != nil {
return &AESf{}, err
}
return &AESf{
password: password,
iter: DefaultIterations,
saltSize: saltSize,
keySize: saltSize * 2,
}, nil
}
func (aesf *AESf) pbkdf2Key(salt []byte) []byte {
return pbkdf2.Key([]byte(aesf.password), salt, aesf.iter, aesf.keySize*2+PwdVerifierSize, sha256.New)
}
func (aesf *AESf) Encrypt(ciphertext io.Writer) (plaintext io.WriteCloser, err error) {
salt := []byte(uniuri.NewLen(aesf.saltSize))
dk := aesf.pbkdf2Key(salt)
block, err := aes.NewCipher(dk[:aesf.keySize])
if err != nil {
return
}
stream := cipher.NewCTR(block, dk[aesf.keySize:aesf.keySize*2])
sha1Mac := hmac.New(sha256.New, dk[aesf.keySize:aesf.keySize*2])
_, err = ciphertext.Write(append(salt, dk[aesf.keySize*2:]...))
if err != nil {
return
}
plaintext = &aesfWriter{s: stream, w: ciphertext, hash: sha1Mac}
return
}
func (aesf *AESf) Decrypt(ciphertext io.Reader) (plaintext io.ReadCloser, err error) {
header := make([]byte, aesf.saltSize+PwdVerifierSize)
lr := io.LimitReader(ciphertext, int64(len(header)))
if _, err = lr.Read(header); err != nil {
return
}
dk := aesf.pbkdf2Key(header[:aesf.saltSize])
if !bytes.Equal(header[aesf.saltSize:], dk[aesf.keySize*2:]) {
return nil, ErrBadPassword
}
block, err := aes.NewCipher(dk[:aesf.keySize])
if err != nil {
return
}
stream := cipher.NewCTR(block, dk[aesf.keySize:aesf.keySize*2])
sha1Mac := hmac.New(sha256.New, dk[aesf.keySize:aesf.keySize*2])
plaintext = &aesfReader{
s: stream,
r: ciphertext,
hash: sha1Mac,
}
return
}