|
| 1 | +package andotp |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "crypto/sha1" |
| 7 | + "encoding/binary" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "math/rand" |
| 11 | + |
| 12 | + "golang.org/x/crypto/pbkdf2" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + IV_LEN int = 12 |
| 17 | + KEY_LEN int = 32 |
| 18 | + ITERATION_LEN int = 4 |
| 19 | + SALT_LEN int = 12 |
| 20 | + MAX_ITERATIONS int = 160000 |
| 21 | + MIN_ITERATIONS int = 140000 |
| 22 | +) |
| 23 | + |
| 24 | +func Encrypt(plaintext []byte, password string) ([]byte, error) { |
| 25 | + |
| 26 | + var finalCipher []byte |
| 27 | + iter := make([]byte, ITERATION_LEN) |
| 28 | + iv := make([]byte, IV_LEN) |
| 29 | + salt := make([]byte, SALT_LEN) |
| 30 | + |
| 31 | + iterations := rand.Intn(MAX_ITERATIONS-MIN_ITERATIONS) + MIN_ITERATIONS |
| 32 | + binary.BigEndian.PutUint32(iter, uint32(iterations)) |
| 33 | + |
| 34 | + _, err := rand.Read(iv) |
| 35 | + if err != nil { |
| 36 | + return nil, FormatError(err.Error()) |
| 37 | + } |
| 38 | + |
| 39 | + _, err = rand.Read(salt) |
| 40 | + if err != nil { |
| 41 | + return nil, FormatError(err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + secretKey := pbkdf2.Key([]byte(password), salt, iterations, KEY_LEN, sha1.New) |
| 45 | + |
| 46 | + block, err := aes.NewCipher(secretKey) |
| 47 | + if err != nil { |
| 48 | + return nil, FormatError(err.Error()) |
| 49 | + } |
| 50 | + |
| 51 | + aesgcm, err := cipher.NewGCM(block) |
| 52 | + if err != nil { |
| 53 | + return nil, FormatError(err.Error()) |
| 54 | + } |
| 55 | + |
| 56 | + cipherText := aesgcm.Seal(nil, iv, plaintext, nil) |
| 57 | + |
| 58 | + finalCipher = append(finalCipher, iter...) |
| 59 | + finalCipher = append(finalCipher, salt...) |
| 60 | + finalCipher = append(finalCipher, iv...) |
| 61 | + finalCipher = append(finalCipher, cipherText...) |
| 62 | + |
| 63 | + return finalCipher, nil |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +func Decrypt(encryptedtext []byte, password string) ([]byte, error) { |
| 68 | + |
| 69 | + iterations := encryptedtext[:ITERATION_LEN] |
| 70 | + salt := encryptedtext[ITERATION_LEN : ITERATION_LEN+SALT_LEN] |
| 71 | + iv := encryptedtext[ITERATION_LEN+SALT_LEN : ITERATION_LEN+SALT_LEN+IV_LEN] |
| 72 | + cipherText := encryptedtext[ITERATION_LEN+SALT_LEN+IV_LEN:] |
| 73 | + iter := int(binary.BigEndian.Uint32(iterations)) |
| 74 | + secretKey := pbkdf2.Key([]byte(password), salt, iter, KEY_LEN, sha1.New) |
| 75 | + |
| 76 | + block, err := aes.NewCipher(secretKey) |
| 77 | + if err != nil { |
| 78 | + return nil, FormatError(err.Error()) |
| 79 | + } |
| 80 | + |
| 81 | + aesgcm, err := cipher.NewGCM(block) |
| 82 | + if err != nil { |
| 83 | + return nil, FormatError(err.Error()) |
| 84 | + } |
| 85 | + |
| 86 | + plaintextbytes, err := aesgcm.Open(nil, iv, cipherText, nil) |
| 87 | + if err != nil { |
| 88 | + return nil, FormatError(err.Error()) |
| 89 | + } |
| 90 | + |
| 91 | + return plaintextbytes, nil |
| 92 | +} |
| 93 | + |
| 94 | +func FormatError(e string) error { |
| 95 | + return fmt.Errorf("error: %s", e) |
| 96 | +} |
| 97 | + |
| 98 | +func ReadFile(file string) ([]byte, error) { |
| 99 | + return ioutil.ReadFile(file) |
| 100 | +} |
0 commit comments