123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package pkcs12
- import (
- "bytes"
- "crypto/sha1"
- "math/big"
- )
- var (
- one = big.NewInt(1)
- )
- func sha1Sum(in []byte) []byte {
- sum := sha1.Sum(in)
- return sum[:]
- }
- func fillWithRepeats(pattern []byte, v int) []byte {
- if len(pattern) == 0 {
- return nil
- }
- outputLen := v * ((len(pattern) + v - 1) / v)
- return bytes.Repeat(pattern, (outputLen+len(pattern)-1)/len(pattern))[:outputLen]
- }
- func pbkdf(hash func([]byte) []byte, u, v int, salt, password []byte, r int, ID byte, size int) (key []byte) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var D []byte
- for i := 0; i < v; i++ {
- D = append(D, ID)
- }
-
-
-
-
- S := fillWithRepeats(salt, v)
-
-
-
-
- P := fillWithRepeats(password, v)
-
- I := append(S, P...)
-
- c := (size + u - 1) / u
-
- A := make([]byte, c*20)
- var IjBuf []byte
- for i := 0; i < c; i++ {
-
-
- Ai := hash(append(D, I...))
- for j := 1; j < r; j++ {
- Ai = hash(Ai)
- }
- copy(A[i*20:], Ai[:])
- if i < c-1 {
-
-
- var B []byte
- for len(B) < v {
- B = append(B, Ai[:]...)
- }
- B = B[:v]
-
-
-
- {
- Bbi := new(big.Int).SetBytes(B)
- Ij := new(big.Int)
- for j := 0; j < len(I)/v; j++ {
- Ij.SetBytes(I[j*v : (j+1)*v])
- Ij.Add(Ij, Bbi)
- Ij.Add(Ij, one)
- Ijb := Ij.Bytes()
-
-
-
- if len(Ijb) > v {
- Ijb = Ijb[len(Ijb)-v:]
- }
- if len(Ijb) < v {
- if IjBuf == nil {
- IjBuf = make([]byte, v)
- }
- bytesShort := v - len(Ijb)
- for i := 0; i < bytesShort; i++ {
- IjBuf[i] = 0
- }
- copy(IjBuf[bytesShort:], Ijb)
- Ijb = IjBuf
- }
- copy(I[j*v:(j+1)*v], Ijb)
- }
- }
- }
- }
-
-
-
- return A[:size]
-
-
-
-
-
- }
|