| 1 |
package domain |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/cipher" |
| 5 |
"crypto/des" |
| 6 |
"crypto/rand" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
) |
| 10 |
|
| 11 |
const initializationVector = "\x01\x23\x45\x67\x89\xAB\xCD\xEF\xFE\xDC\xBA\x98\x76\x54\x32\x10\xF0\xE1\xD2\xC3" |
| 12 |
|
| 13 |
func encryptMessage(message fmt.Stringer) (string, error) { |
| 14 |
block, err := des.NewCipher([]byte(initializationVector)) |
| 15 |
if err != nil { |
| 16 |
return "", err |
| 17 |
} |
| 18 |
ciphertext := make([]byte, des.BlockSize+len(message.String())) |
| 19 |
iv := ciphertext[:des.BlockSize] |
| 20 |
if _, err := io.ReadFull(rand.Reader, iv); err != nil { |
| 21 |
panic(err) |
| 22 |
} |
| 23 |
|
| 24 |
mode := cipher.NewCBCEncrypter(block, iv) |
| 25 |
mode.CryptBlocks(ciphertext[des.BlockSize:], []byte(message.String())) |
| 26 |
|
| 27 |
// It's important to remember that ciphertexts must be authenticated |
| 28 |
// (i.e. by using crypto/hmac) as well as being encrypted in order to |
| 29 |
// be secure. |
| 30 |
|
| 31 |
return fmt.Sprintf("%x", ciphertext), nil |
| 32 |
} |