Skip to content

Commit

Permalink
rename DataBasic and DataExtended.Component to 'Components'
Browse files Browse the repository at this point in the history
  • Loading branch information
skythen authored and Steven committed May 25, 2021
1 parent 8a76079 commit dbcc709
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
25 changes: 13 additions & 12 deletions key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const (

// ComponentBasic is a key component in basic format.
type ComponentBasic struct {
Type byte // Type of the Key Component.
Block ComponentBlock // Block with a Key Component.
Type byte // Type of the Key Components.
Block ComponentBlock // Block with a Key Components.
KCV []byte // Optional Key Check Value.
}

Expand All @@ -85,6 +85,7 @@ type ComponentBasic struct {
// If you want to provide details regarding key usage and key access, use NewComponentExtended.
func NewComponentBasic(keyComponentType byte, keyComponentValue, kcv []byte, paddingLength int) *ComponentBasic {
cb := &ComponentBasic{}

var block ComponentBlock

cb.Type = keyComponentType
Expand Down Expand Up @@ -115,7 +116,7 @@ type ComponentBlock interface {
// padding that has been applied to the key component for encryption.
type ComponentPaddedBlock struct {
LengthComponent int
Value []byte // For a public key component, the key component value does not need to be encrypted and the Key Component Block only contains the clear-text key component value.
Value []byte // For a public key component, the key component value does not need to be encrypted and the Key Components Block only contains the clear-text key component value.
}

// Bytes implements the ComponentBlock interface and encodes ComponentPaddedBlock on LV-encoded bytes
Expand Down Expand Up @@ -301,7 +302,7 @@ func UsageForType(usage UsageType) *UsageQualifier {

// DataBasic represents the data field of a PUT KEY command and contains a list of ComponentBasic.
type DataBasic struct {
Component []ComponentBasic
Components []ComponentBasic
}

// Bytes encodes DataBasic on LV-encoded bytes.
Expand All @@ -311,22 +312,22 @@ func (db DataBasic) Bytes() ([]byte, error) {
err error
)

bBlocks := make([][]byte, len(db.Component))
bBlockLength := make([][]byte, len(db.Component))
bBlocks := make([][]byte, len(db.Components))
bBlockLength := make([][]byte, len(db.Components))

// calculate length
for i, component := range db.Component {
for i, component := range db.Components {
// length key type length field = 1 and length key check value length field = 1
length += 2

bBlocks[i], err = component.Block.Bytes()
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("invalid key component block %d/%d", i, len(db.Component)))
return nil, errors.Wrap(err, fmt.Sprintf("invalid key component block %d/%d", i, len(db.Components)))
}

bBlockLength[i], err = util.BuildGPBerLength(uint(len(bBlocks[i])))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("build BER length for Key Component %d/%d", i, len(db.Component)))
return nil, errors.Wrap(err, fmt.Sprintf("build BER length for Key Components %d/%d", i, len(db.Components)))
}

length += len(bBlockLength[i])
Expand All @@ -336,7 +337,7 @@ func (db DataBasic) Bytes() ([]byte, error) {

bytes := make([]byte, 0, length)

for i, com := range db.Component {
for i, com := range db.Components {
bytes = append(bytes, com.Type)
bytes = append(bytes, bBlockLength[i]...)
bytes = append(bytes, bBlocks[i]...)
Expand Down Expand Up @@ -366,15 +367,15 @@ func (de DataExtended) Bytes() ([]byte, error) {
for i, component := range de.Components {
length += 6

// Length of Key Component Block
// Length of Key Components Block
bBlocks[i], err = component.Block.Bytes()
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("invalid key component block %d/%d", i+1, len(de.Components)))
}

bBlockLength[i], err = util.BuildGPBerLength(uint(len(bBlocks[i])))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("build BER length for Key Component %d/%d", i, len(de.Components)))
return nil, errors.Wrap(err, fmt.Sprintf("build BER length for Key Components %d/%d", i, len(de.Components)))
}

bKeyUsage[i] = component.UsageQualifier.Bytes()
Expand Down
8 changes: 4 additions & 4 deletions key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func TestKeyDataBasic_Bytes(t *testing.T) {
{
name: "Convert to byte",
keyDataBasic: DataBasic{
Component: []ComponentBasic{
Components: []ComponentBasic{
{
Type: TypeDES,
Block: ComponentUnpaddedBlock{[]byte{0xA1, 0xA2, 0xA3, 0xA4, 0xA1, 0xA2, 0xA3, 0xA4, 0xA1, 0xA2, 0xA3, 0xA4, 0xA1, 0xA2, 0xA3, 0xA4}},
Expand All @@ -379,7 +379,7 @@ func TestKeyDataBasic_Bytes(t *testing.T) {
{
name: "Error: invalid key component block",
keyDataBasic: DataBasic{
Component: []ComponentBasic{
Components: []ComponentBasic{
{
Type: TypeDES,
Block: ComponentPaddedBlock{
Expand All @@ -395,7 +395,7 @@ func TestKeyDataBasic_Bytes(t *testing.T) {
{
name: "Error: invalid key component block",
keyDataBasic: DataBasic{
Component: []ComponentBasic{
Components: []ComponentBasic{
{
Type: TypeDES,
Block: ComponentPaddedBlock{
Expand All @@ -411,7 +411,7 @@ func TestKeyDataBasic_Bytes(t *testing.T) {
{
name: "Error: invalid length of key component block",
keyDataBasic: DataBasic{
Component: []ComponentBasic{
Components: []ComponentBasic{
{
Type: TypeDES,
Block: ComponentUnpaddedBlock{make([]byte, 65536)},
Expand Down

0 comments on commit dbcc709

Please sign in to comment.