Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make errors faithful #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
avoid computing new v3 keys in case of error
  • Loading branch information
XaviFP committed Jul 13, 2022
commit 9559b91adf61e9e92a1eac02a79e43bc5d13e507
30 changes: 12 additions & 18 deletions v3_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ func NewV3AsymmetricPublicKeyFromHex(hexEncoded string) (V3AsymmetricPublicKey,
publicKeyBytes, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV3AsymmetricSecretKey().Public(), err

return V3AsymmetricPublicKey{}, err
}

return NewV3AsymmetricPublicKeyFromBytes(publicKeyBytes)
Expand All @@ -35,9 +34,8 @@ func NewV3AsymmetricPublicKeyFromHex(hexEncoded string) (V3AsymmetricPublicKey,
// NewV3AsymmetricPublicKeyFromBytes Construct a v3 public key from bytes
func NewV3AsymmetricPublicKeyFromBytes(publicKeyBytes []byte) (V3AsymmetricPublicKey, error) {
if len(publicKeyBytes) != 49 {
// even though we return error, return a random key here rather than
// a nil key
return NewV3AsymmetricSecretKey().Public(), errors.New("Key incorrect length")

return V3AsymmetricPublicKey{}, errors.New("Key incorrect length")
}

publicKey := new(ecdsa.PublicKey)
Expand Down Expand Up @@ -99,9 +97,8 @@ func NewV3AsymmetricSecretKeyFromHex(hexEncoded string) (V3AsymmetricSecretKey,
secretBytes, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV3AsymmetricSecretKey(), err

return V3AsymmetricSecretKey{}, err
}

return NewV3AsymmetricSecretKeyFromBytes(secretBytes)
Expand All @@ -110,9 +107,8 @@ func NewV3AsymmetricSecretKeyFromHex(hexEncoded string) (V3AsymmetricSecretKey,
// NewV3AsymmetricSecretKeyFromBytes creates a secret key from bytes
func NewV3AsymmetricSecretKeyFromBytes(secretBytes []byte) (V3AsymmetricSecretKey, error) {
if len(secretBytes) != 48 {
// even though we return error, return a random key here rather than
// a nil key
return NewV3AsymmetricSecretKey(), errors.New("Key incorrect length")

return V3AsymmetricSecretKey{}, errors.New("Key incorrect length")
}

privateKey := new(ecdsa.PrivateKey)
Expand Down Expand Up @@ -154,9 +150,8 @@ func (k V3SymmetricKey) ExportBytes() []byte {
func V3SymmetricKeyFromHex(hexEncoded string) (V3SymmetricKey, error) {
bytes, err := hex.DecodeString(hexEncoded)
if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV3SymmetricKey(), err

return V3SymmetricKey{}, err
}

return V3SymmetricKeyFromBytes(bytes)
Expand All @@ -165,9 +160,8 @@ func V3SymmetricKeyFromHex(hexEncoded string) (V3SymmetricKey, error) {
// V3SymmetricKeyFromBytes constructs a key from bytes
func V3SymmetricKeyFromBytes(bytes []byte) (V3SymmetricKey, error) {
if len(bytes) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV3SymmetricKey(), errors.New("Key incorrect length")

return V3SymmetricKey{}, errors.New("Key incorrect length")
}

var material [32]byte
Expand Down