-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc_crypto.go
62 lines (46 loc) · 1.04 KB
/
misc_crypto.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
58
59
60
61
62
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"math"
)
func aesDecrypt(ciphertext []byte, key []byte, iv []byte) (plaintext []byte) {
block, err := aes.NewCipher(key)
panicFail(err)
if len(ciphertext) < aes.BlockSize {
err = fmt.Errorf("ciphertext too short")
panicFail(err)
}
if len(ciphertext)%aes.BlockSize != 0 {
err = fmt.Errorf("ciphertext has wrong size")
panicFail(err)
}
deciphered := make([]byte, len(ciphertext))
cbc := cipher.NewCBCDecrypter(block, iv)
cbc.CryptBlocks(deciphered, ciphertext)
plaintext = aesUnpad(deciphered)
return
}
func aesUnpad(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
if unpadding >= length {
return src
}
return src[:(length - unpadding)]
}
func Shannon(value []byte) (bits int) {
frq := make(map[byte]float64)
//get frequency of characters
for _, i := range value {
frq[i]++
}
var sum float64
for _, v := range frq {
f := v / float64(len(value))
sum += f * math.Log2(f)
}
bits = int(math.Ceil(sum*-1)) * len(value)
return
}