Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation import/export JWK implementation
- Loading branch information
1 parent
d8ebd3f
commit e284c5e
Showing
11 changed files
with
227 additions
and
74 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
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
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
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,64 @@ | ||
package webcrypto | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// JWK represents a JSON Web Key (JWK) key. | ||
type JWK map[string]interface{} | ||
|
||
// Set sets a key-value pair in the JWK. | ||
func (jwk *JWK) Set(key string, value interface{}) { | ||
(*jwk)[key] = value | ||
} | ||
|
||
// ExtractSymmetricJWK extracts the symmetric key from a given JWK key (JSON data). | ||
func ExtractSymmetricJWK(jsonKeyData []byte) ([]byte, error) { | ||
var key JWK | ||
err := json.Unmarshal(jsonKeyData, &key) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse JWK key: %w", err) | ||
} | ||
|
||
k, ok := key["k"] | ||
if !ok || k == "" { | ||
return nil, errors.New("invalid JWK key") | ||
} | ||
|
||
kv, ok := k.(string) | ||
if !ok { | ||
return nil, errors.New("invalid JWK key") | ||
} | ||
|
||
return base64.RawStdEncoding.DecodeString(kv) | ||
} | ||
|
||
// ExportSymmetricJWK exports a symmetric key as a map of JWK key parameters. | ||
func ExportSymmetricJWK(key *CryptoKey) (*JWK, error) { | ||
// currently, handle is key in the form of a byte slice | ||
rawKey, ok := key.handle.([]byte) | ||
if !ok { | ||
return nil, errors.New("key's handle isn't a byte slice") | ||
} | ||
|
||
exported := &JWK{ | ||
"kty": "oct", | ||
"key_ops": key.Usages, | ||
"ext": key.Extractable, | ||
"k": base64.RawStdEncoding.EncodeToString(rawKey), | ||
} | ||
|
||
switch alg := key.Algorithm.(type) { | ||
case hashAlgorithm: | ||
exported.Set("alg", fmt.Sprintf("HS"+alg.hash()[4:])) | ||
case namedAlgorithm: | ||
exported.Set("alg", fmt.Sprintf("A%d%s", (8*len(rawKey)), alg.alg()[4:])) | ||
default: | ||
return nil, errors.New("unsupported algorithm") | ||
} | ||
|
||
return exported, nil | ||
} |
Oops, something went wrong.