Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 17, 2024
1 parent ce944d2 commit 4759c0f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkcs/pbes1/cipher_blockcbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func (this CipherBlockCBC) WithSaltSize(saltSize int) CipherBlockCBC {
return this
}

// 设置 saltSize
func (this CipherBlockCBC) WithKeySize(keySize int) CipherBlockCBC {
this.keySize = keySize

return this
}

// 设置 derivedKeyFunc
func (this CipherBlockCBC) WithDerivedKeyFunc(derivedKeyFunc DerivedKeyFunc) CipherBlockCBC {
this.derivedKeyFunc = derivedKeyFunc
Expand Down
48 changes: 48 additions & 0 deletions pkcs/pbes1/cipher_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"crypto/cipher"
"encoding/asn1"

"golang.org/x/crypto/cast5"
"golang.org/x/crypto/twofish"

cryptobin_md2 "github.com/deatil/go-cryptobin/hash/md2"
cryptobin_rc2 "github.com/deatil/go-cryptobin/cipher/rc2"
cryptobin_des "github.com/deatil/go-cryptobin/cipher/des"
Expand All @@ -28,12 +31,21 @@ var (
oidPbeWithMD5AndRC2_64 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 6}
oidPbeWithSHA1AndDES = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 10}
oidPbeWithSHA1AndRC2_64 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 11}

oidPbeWithMD5AndCAST5 = asn1.ObjectIdentifier{1, 2, 840, 113533, 7, 66, 12}
oidPbeWithSHAAndTwofish = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 21}
)

var (
newRC2Cipher = func(key []byte) (cipher.Block, error) {
return cryptobin_rc2.NewCipher(key, len(key)*8)
}
newCAST5Cipher = func(key []byte) (cipher.Block, error) {
return cast5.NewCipher(key)
}
newTwofishCipher = func(key []byte) (cipher.Block, error) {
return twofish.NewCipher(key)
}
)

// pkcs12
Expand Down Expand Up @@ -182,6 +194,35 @@ var SHA1AndRC2_64 = CipherBlockCBC{
needBmpPass: false,
}

var MD5AndCAST5 = CipherBlockCBC{
cipherFunc: newCAST5Cipher,
hashFunc: md5.New,
derivedKeyFunc: DerivedKeyPkcs12,
saltSize: cast5.BlockSize,
keySize: 16,
blockSize: cast5.BlockSize,
iterationCount: 2048,
oid: oidPbeWithMD5AndCAST5,
hasKeyLength: false,
needBmpPass: false,
}
var SHAAndTwofish = CipherBlockCBC{
cipherFunc: newTwofishCipher,
hashFunc: sha1.New,
derivedKeyFunc: DerivedKeyPkcs12,
saltSize: twofish.BlockSize,
keySize: 16,
blockSize: twofish.BlockSize,
iterationCount: 2048,
oid: oidPbeWithSHAAndTwofish,
hasKeyLength: true,
needBmpPass: false,
}

var SHAAndTwofish_16 = SHAAndTwofish.WithKeySize(16)
var SHAAndTwofish_24 = SHAAndTwofish.WithKeySize(24)
var SHAAndTwofish_32 = SHAAndTwofish.WithKeySize(32)

func init() {
// pkcs12
AddCipher(oidPbeWithSHA1And3DES, func() Cipher {
Expand Down Expand Up @@ -222,4 +263,11 @@ func init() {
AddCipher(oidPbeWithSHA1AndRC2_64, func() Cipher {
return SHA1AndRC2_64
})

AddCipher(oidPbeWithMD5AndCAST5, func() Cipher {
return MD5AndCAST5
})
AddCipher(oidPbeWithSHAAndTwofish, func() Cipher {
return SHAAndTwofish
})
}
7 changes: 7 additions & 0 deletions pkcs/pbes1/cipher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func Test_Ciphers(t *testing.T) {
test_cipher(t, SHA1AndDES, "SHA1AndDES", []byte("hsdfrt5t"))
test_cipher(t, SHA1AndRC2_64, "SHA1AndRC2_64", []byte("hsdfrt5t"))

test_cipher(t, MD5AndCAST5, "MD5AndCAST5", []byte("hsdfrt5thsdfrt5t"))

test_cipher(t, SHAAndTwofish, "SHAAndTwofish", []byte("hsdfrt5thsdfrt5t"))
test_cipher(t, SHAAndTwofish_16, "SHAAndTwofish_16", []byte("hsdfrt5thsdfrt5t"))
test_cipher(t, SHAAndTwofish_24, "SHAAndTwofish_24", []byte("hsdfrt5thsdfrt5t"))
test_cipher(t, SHAAndTwofish_32, "SHAAndTwofish_32", []byte("hsdfrt5thsdfrt5t"))

}


0 comments on commit 4759c0f

Please sign in to comment.