forked from mitch000001/go-hbci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
32 lines (26 loc) · 847 Bytes
/
encryption.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
package domain
import (
"crypto/cipher"
"crypto/des"
"crypto/rand"
"fmt"
"io"
)
const initializationVector = "\x01\x23\x45\x67\x89\xAB\xCD\xEF\xFE\xDC\xBA\x98\x76\x54\x32\x10\xF0\xE1\xD2\xC3"
func encryptMessage(message fmt.Stringer) (string, error) {
block, err := des.NewCipher([]byte(initializationVector))
if err != nil {
return "", err
}
ciphertext := make([]byte, des.BlockSize+len(message.String()))
iv := ciphertext[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[des.BlockSize:], []byte(message.String()))
// It's important to remember that ciphertexts must be authenticated
// (i.e. by using crypto/hmac) as well as being encrypted in order to
// be secure.
return fmt.Sprintf("%x", ciphertext), nil
}