-
Notifications
You must be signed in to change notification settings - Fork 4
/
blowfish.go
64 lines (56 loc) · 1.63 KB
/
blowfish.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
63
64
package crypt
import (
"crypto/cipher"
"fmt"
)
const blowfishBlockSize = 8
var Blowfish cryptBlowfish
type cryptBlowfish struct{}
func (cryptBlowfish) Encrypt(plaintext, key []byte) ([]byte, error) {
c, err := NewBlowfish(key)
if err != nil {
return nil, err
}
return c.Encrypt(plaintext)
}
func (cryptBlowfish) Decrypt(ciphertext, key []byte) ([]byte, error) {
c, err := NewBlowfish(key)
if err != nil {
return nil, err
}
return c.Decrypt(ciphertext)
}
func blowfishEncrypt(src []byte, block cipher.Block) (ciphertext []byte, err error) {
var plaintext []byte
var b = make([]byte, blowfishBlockSize)
var size int
if len(src) % blowfishBlockSize != 0 {
if plaintext, err = Padding(PAD_ZEROPADDING, src, blowfishBlockSize); err != nil {
return nil, err
}
} else {
plaintext = append([]byte{}, src...)
}
size = len(plaintext) / blowfishBlockSize
ciphertext = make([]byte, 0, size * blowfishBlockSize)
for i := 0; i < size; i++ {
block.Encrypt(b, plaintext[i*blowfishBlockSize:(i+1)*blowfishBlockSize])
ciphertext = append(ciphertext, b...)
}
return
}
func blowfishDecrypt(src []byte, block cipher.Block) (plaintext []byte, err error) {
var b = make([]byte, blowfishBlockSize)
var size int
if len(src) % blowfishBlockSize != 0 {
return nil, fmt.Errorf("crypt Blowfish.Decrypt: ciphertext length must equal block size")
}
size = len(src) / blowfishBlockSize
plaintext = make([]byte, 0, size)
for i := 0; i < size; i++ {
block.Decrypt(b, src[i*blowfishBlockSize:(i+1)*blowfishBlockSize])
plaintext = append(plaintext, b...)
}
plaintext, err = UnPadding(PAD_ZEROPADDING, plaintext, blowfishBlockSize)
return
}