You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/crypto/pkcs_test.go

107 lines
4.7 KiB
Go

// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package crypto
import (
"testing"
"github.com/q191201771/naza/pkg/assert"
)
var (
goldenOrig = "1223334444aaaaabbbbbb"
goldenEnSlice7 = [][]byte{
[]byte{16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
[]byte{49, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
[]byte{49, 50, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
[]byte{49, 50, 50, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13},
[]byte{49, 50, 50, 51, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
[]byte{49, 50, 50, 51, 51, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11},
[]byte{49, 50, 50, 51, 51, 51, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
[]byte{49, 50, 50, 51, 51, 51, 52, 9, 9, 9, 9, 9, 9, 9, 9, 9},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 8, 8, 8, 8, 8, 8, 8, 8},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 7, 7, 7, 7, 7, 7, 7},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 6, 6, 6, 6, 6, 6},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 5, 5, 5, 5, 5},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 4, 4, 4, 4},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 3, 3, 3},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 2, 2},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 1},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11},
}
goldenEnSlice5 = [][]byte{
[]byte{8, 8, 8, 8, 8, 8, 8, 8},
[]byte{49, 7, 7, 7, 7, 7, 7, 7},
[]byte{49, 50, 6, 6, 6, 6, 6, 6},
[]byte{49, 50, 50, 5, 5, 5, 5, 5},
[]byte{49, 50, 50, 51, 4, 4, 4, 4},
[]byte{49, 50, 50, 51, 51, 3, 3, 3},
[]byte{49, 50, 50, 51, 51, 51, 2, 2},
[]byte{49, 50, 50, 51, 51, 51, 52, 1},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 8, 8, 8, 8, 8, 8, 8, 8},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 7, 7, 7, 7, 7, 7, 7},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 6, 6, 6, 6, 6, 6},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 5, 5, 5, 5, 5},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 4, 4, 4, 4},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 3, 3, 3},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 2, 2},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 1},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 8, 8, 8, 8, 8, 8, 8, 8},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 7, 7, 7, 7, 7, 7, 7},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 6, 6, 6, 6, 6, 6},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 5, 5, 5, 5, 5},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 4, 4, 4, 4},
[]byte{49, 50, 50, 51, 51, 51, 52, 52, 52, 52, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 3, 3, 3},
}
)
func TestPkcs7(t *testing.T) {
for i := 0; i < len(goldenOrig)+1; i++ {
orig := []byte(goldenOrig[:i])
//nazalog.Info(hex.Dump(orig))
enbuf := EncryptPkcs7(orig, 16)
//nazalog.Info(hex.Dump(enbuf))
assert.Equal(t, goldenEnSlice7[i], enbuf)
debuf, err := DecryptPkcs7(enbuf)
//nazalog.Info(hex.Dump(debuf))
assert.Equal(t, nil, err)
assert.Equal(t, orig, debuf)
}
// corner case
_, err := DecryptPkcs7(nil)
assert.Equal(t, ErrPkcs, err)
_, err = DecryptPkcs7([]byte{16})
assert.Equal(t, ErrPkcs, err)
}
func TestPkcs5(t *testing.T) {
for i := 0; i < len(goldenOrig)+1; i++ {
orig := []byte(goldenOrig[:i])
//nazalog.Info(hex.Dump(orig))
enbuf := EncryptPkcs5(orig)
//nazalog.Info(hex.Dump(enbuf))
assert.Equal(t, goldenEnSlice5[i], enbuf)
debuf, err := DecryptPkcs5(enbuf)
//nazalog.Info(hex.Dump(debuf))
assert.Equal(t, nil, err)
assert.Equal(t, orig, debuf)
}
}