-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.go
More file actions
45 lines (34 loc) · 959 Bytes
/
crypto.go
File metadata and controls
45 lines (34 loc) · 959 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
36
37
38
39
40
41
42
43
44
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/hex"
"errors"
"golang.org/x/crypto/pbkdf2"
)
const (
saltSize = 16
keySize = 32
)
func decryptOld(ciphertext, password string) (string, error) {
ciphertextBytes, err := hex.DecodeString(ciphertext)
if err != nil {
return "", err
}
if len(ciphertextBytes) < saltSize+aes.BlockSize {
return "", errors.New("Ciphertext is too short")
}
salt := ciphertextBytes[:saltSize]
ciphertextBytes = ciphertextBytes[saltSize:]
key := pbkdf2.Key([]byte(password), salt, 4096, keySize, sha1.New)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
iv := ciphertextBytes[:aes.BlockSize]
ciphertextBytes = ciphertextBytes[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertextBytes, ciphertextBytes)
return string(ciphertextBytes), nil
}