storj/pkg/pkcrypto/signing_test.go
ethanadams 16e3b77cf5
Enable Scopelint Linter (#2049)
* added scopelint and correcte issues found

* corrected scopelint issue

* made updates based on Ivan's suggestions

Most were around naming conventions
Some were false positives, but I kept them since the test.Run could eventually be changed to run in parallel, which could cause a bug
Others were false positives.  Added // nolint: scopelint
2019-05-29 09:30:16 -04:00

70 lines
1.7 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package pkcrypto
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSigningAndVerifyingECDSA(t *testing.T) {
tests := []struct {
name string
data string
}{
{"empty", ""},
{"single byte", "C"},
{"longnulls", string(make([]byte, 2000))},
}
for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
privKey, err := GeneratePrivateECDSAKey(authECCurve)
assert.NoError(t, err)
pubKey := PublicKeyFromPrivate(privKey)
// test signing and verifying a hash of the data
sig, err := HashAndSign(privKey, []byte(test.data))
assert.NoError(t, err)
err = HashAndVerifySignature(pubKey, []byte(test.data), sig)
assert.NoError(t, err)
// test signing and verifying the data directly
sig, err = SignWithoutHashing(privKey, []byte(test.data))
assert.NoError(t, err)
err = VerifySignatureWithoutHashing(pubKey, []byte(test.data), sig)
assert.NoError(t, err)
})
}
}
func TestSigningAndVerifyingRSA(t *testing.T) {
privKey, err := GeneratePrivateRSAKey(StorjRSAKeyBits)
assert.NoError(t, err)
pubKey := PublicKeyFromPrivate(privKey)
tests := []struct {
name string
data string
}{
{"empty", ""},
{"single byte", "C"},
{"longnulls", string(make([]byte, 2000))},
}
for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
// test signing and verifying a hash of the data
sig, err := HashAndSign(privKey, []byte(test.data))
assert.NoError(t, err)
err = HashAndVerifySignature(pubKey, []byte(test.data), sig)
assert.NoError(t, err)
// don't test signing and verifying the data directly, as RSA can't
// handle messages of arbitrary size
})
}
}