-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Signature verification (#1)
* Added working signatures * Added some signature tests
- Loading branch information
Showing
8 changed files
with
366 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ocmf_go | ||
|
||
type BuilderOption func(*Builder) | ||
|
||
func WithSignatureAlgorithm(algorithm SignatureAlgorithm) BuilderOption { | ||
return func(b *Builder) { | ||
if isValidSignatureAlgorithm(algorithm) { | ||
b.signature.Algorithm = algorithm | ||
} | ||
} | ||
} | ||
|
||
func WithSignatureEncoding(encoding SignatureEncoding) BuilderOption { | ||
return func(b *Builder) { | ||
if isValidSignatureEncoding(encoding) { | ||
b.signature.Encoding = encoding | ||
} | ||
} | ||
} | ||
|
||
func WithSignature(signature Signature) BuilderOption { | ||
return func(b *Builder) { | ||
err := signature.Validate() | ||
if err != nil { | ||
return | ||
} | ||
|
||
b.signature = signature | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package ocmf_go | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type builderOptsTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *builderOptsTestSuite) TestWithSignatureAlgorithm() { | ||
tests := []struct { | ||
name string | ||
algorithm SignatureAlgorithm | ||
want bool | ||
}{ | ||
{ | ||
name: "ECDSA-secp192k1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAsecp192k1SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "ECDSA-secp256k1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAsecp256k1SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "ECDSA-secp384r1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAsecp384r1SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "ECDSA-brainpool256r1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAbrainpool256r11SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "ECDSA-secp256r1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAsecp256r1SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "ECDSA-secp192r1-SHA256", | ||
algorithm: SignatureAlgorithmECDSAsecp192r1SHA256, | ||
want: true, | ||
}, | ||
{ | ||
name: "Unknown algorithm", | ||
algorithm: SignatureAlgorithm("ABCD"), | ||
want: false, | ||
}, | ||
{ | ||
name: "Empty algorithm", | ||
algorithm: SignatureAlgorithm(""), | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
s.T().Run(tt.name, func(t *testing.T) { | ||
curve := elliptic.P256() | ||
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader) | ||
s.Require().NoError(err) | ||
|
||
builder := NewBuilder(privateKey, WithSignatureAlgorithm(tt.algorithm)) | ||
|
||
if tt.name == "Unknown algorithm" || tt.name == "Empty algorithm" { | ||
s.NotEqual(tt.algorithm, builder.signature.Algorithm) | ||
} else { | ||
s.Equal(tt.algorithm, builder.signature.Algorithm) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (s *builderOptsTestSuite) TestWithWithSignatureEncoding() { | ||
tests := []struct { | ||
name string | ||
encoding SignatureEncoding | ||
}{ | ||
{ | ||
name: "Base64 encoding", | ||
encoding: SignatureEncodingBase64, | ||
}, | ||
{ | ||
name: "Hex encoding", | ||
encoding: SignatureEncodingHex, | ||
}, | ||
{ | ||
name: "Empty encoding", | ||
encoding: SignatureEncoding(""), | ||
}, | ||
{ | ||
name: "Unknown encoding", | ||
encoding: SignatureEncoding("ABDD"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
s.T().Run(tt.name, func(t *testing.T) { | ||
builder := NewBuilder(nil, WithSignatureEncoding(tt.encoding)) | ||
|
||
if tt.encoding == SignatureEncodingBase64 || tt.encoding == SignatureEncodingHex { | ||
s.Equal(tt.encoding, builder.signature.Encoding) | ||
} else { | ||
s.NotEqual(tt.encoding, builder.signature.Encoding) | ||
} | ||
}) | ||
} | ||
} | ||
func TestBuilderOpts(t *testing.T) { | ||
suite.Run(t, new(builderOptsTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.