-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
57 lines (46 loc) · 1.14 KB
/
example_test.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
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
package ecb_test
import (
"crypto/aes"
"fmt"
"github.com/clfs/ecb"
)
func ExampleNewEncrypter() {
key := []byte{
0x65, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x65,
0x67, 0x67, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64,
}
plaintext := []byte("exampleplaintext")
if len(plaintext)%aes.BlockSize != 0 {
panic("plaintext is not a multiple of the block size")
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
mode := ecb.NewEncrypter(block)
ciphertext := make([]byte, len(plaintext))
mode.CryptBlocks(ciphertext, plaintext)
fmt.Printf("%x\n", ciphertext)
// Output:
// 188dc8b928ae0a13b0e9ab01127fd341
}
func ExampleNewDecrypter() {
key := []byte{
0x65, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x65,
0x67, 0x67, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64,
}
ciphertext := []byte{
0x18, 0x8d, 0xc8, 0xb9, 0x28, 0xae, 0x0a, 0x13,
0xb0, 0xe9, 0xab, 0x01, 0x12, 0x7f, 0xd3, 0x41,
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
mode := ecb.NewDecrypter(block)
plaintext := make([]byte, len(ciphertext))
mode.CryptBlocks(plaintext, ciphertext)
fmt.Printf("%s\n", plaintext)
// Output:
// exampleplaintext
}