Skip to content

Commit

Permalink
simplify the PublicKeyFromString function
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Mar 31, 2020
1 parent 3741d45 commit 28bf5fc
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,18 @@ func Ed25519PrivateKeyReadPem(source io.Reader, passphrase string) (Ed25519Priva

// Ed25519PublicKeyFromString recovers an Ed25519PublicKey from its text-encoded representation.
func Ed25519PublicKeyFromString(s string) (Ed25519PublicKey, error) {
switch len(s) {
case 64: // raw public key
bytes, err := hex.DecodeString(s)
if err != nil {
return Ed25519PublicKey{}, err
}

return Ed25519PublicKey{bytes}, nil
sLen := len(s)
if sLen != 64 && sLen != 88 {
return Ed25519PublicKey{}, newErrBadKeyf("invalid public key '%v' string with length %v", s, sLen)
}

case 88: // DER encoded public key
if strings.HasPrefix(strings.ToLower(s), ed25519PubKeyPrefix) {
pk, err := Ed25519PublicKeyFromString(s[24:])
if err != nil {
return Ed25519PublicKey{}, err
}
return pk, nil
}
keyStr := strings.TrimPrefix(strings.ToLower(s), ed25519PubKeyPrefix)
bytes, err := hex.DecodeString(keyStr)
if err != nil {
return Ed25519PublicKey{}, err
}
return Ed25519PublicKey{}, newErrBadKeyf("invalid public key '%v' string with length %v", s, len(s))

return Ed25519PublicKey{bytes}, nil
}

// Ed25519PublicKeyFromBytes constructs a known Ed25519PublicKey from its text-encoded representation.
Expand Down

0 comments on commit 28bf5fc

Please sign in to comment.