Skip to content

Commit

Permalink
add LoadEd25519PublicKey func
Browse files Browse the repository at this point in the history
In this commit we add a LoadEd25519PublicKey func for loading
ed25519 keys in PrivateJSON format from a ed25519 public key file
  • Loading branch information
shibumi committed Jun 23, 2020
1 parent 4969540 commit 514e090
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions in_toto/keylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,53 @@ func VerifyEd25519Signature(key Key, sig Signature, data []byte) error {
}
return nil
}

/* LoadEd25519PublicKey loads a ed25519 pub key file
and parses it via ParseEd25519FromPrivateJSON.
The pub key file has to be in the in-toto PrivateJSON format
For example:
{
"keytype": "ed25519",
"scheme": "ed25519",
"keyid_hash_algorithms":
[
"sha256",
"sha512"
],
"keyval":
{
"public": "e8912b58f47ae04a65d7437e3c82eb361f82d952b4d1b3dc5d90c6f37d7aac70"
}
}
*/

func (k *Key) LoadEd25519PublicKey(path string) (err error) {
keyFile, err := os.Open(path)
if err != nil {
return err
}
defer func() {
if closeErr := keyFile.Close(); closeErr != nil {
err = closeErr
}
}()

keyBytes, err := ioutil.ReadAll(keyFile)
if err != nil {
return err
}
// contrary to LoadRSAPublicKey we use the returned key object
keyObj, err := ParseEd25519FromPrivateJSON(string(keyBytes))
if err != nil {
return err
}
// I am not sure if there is a faster way to fill the Key struct
// without touching the ParseEd25519FromPrivateJSON function
k.KeyId = keyObj.KeyId
k.KeyType = keyObj.KeyType
k.KeyIdHashAlgorithms = keyObj.KeyIdHashAlgorithms
k.KeyVal = keyObj.KeyVal
k.Scheme = keyObj.Scheme
return nil
}

0 comments on commit 514e090

Please sign in to comment.