From c8c80aad0dfb824ebc5ce7b35d28d066b4596cd8 Mon Sep 17 00:00:00 2001 From: oleiade Date: Fri, 14 Apr 2023 14:29:39 +0200 Subject: [PATCH] Rename the HMAC-related structures following the Go naming conventions Instead of WebCrypto's --- webcrypto/hmac.go | 44 +++++++++++++++++++------------------- webcrypto/key.go | 4 ++-- webcrypto/params.go | 11 ---------- webcrypto/subtle_crypto.go | 6 +++--- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/webcrypto/hmac.go b/webcrypto/hmac.go index 055ec4f..f3ce684 100644 --- a/webcrypto/hmac.go +++ b/webcrypto/hmac.go @@ -12,9 +12,9 @@ import ( "gopkg.in/guregu/null.v3" ) -// HmacKeyGenParams represents the object that should be passed as the algorithm parameter +// HMACKeyGenParams represents the object that should be passed as the algorithm parameter // into `SubtleCrypto.GenerateKey`, when generating an HMAC key. -type HmacKeyGenParams struct { +type HMACKeyGenParams struct { Algorithm // Hash represents the name of the digest function to use. You can @@ -30,7 +30,7 @@ type HmacKeyGenParams struct { Length null.Int `json:"length"` } -// newHmacKeyGenParams creates a new HmacKeyGenParams object, from the normalized +// newHMACKeyGenParams creates a new HMACKeyGenParams object, from the normalized // algorithm, and the params parameters passed by the user. // // It handles the logic of extracting the hash algorithm from the params object, @@ -39,7 +39,7 @@ type HmacKeyGenParams struct { // not present as described in the hmac `generateKey` [specification]. // // [specification]: https://www.w3.org/TR/WebCryptoAPI/#hmac-operations -func newHmacKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Value) (*HmacKeyGenParams, error) { +func newHMACKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Value) (*HMACKeyGenParams, error) { // The specification doesn't explicitly tell us what to do if the // hash field is not present, but we assume it's a mandatory field // and throw an error if it's not present. @@ -71,7 +71,7 @@ func newHmacKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Val length = null.IntFrom(algorithmLengthValue.ToInteger()) } - return &HmacKeyGenParams{ + return &HMACKeyGenParams{ Algorithm: normalized, Hash: normalizedHash, Length: length, @@ -79,7 +79,7 @@ func newHmacKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Val } // GenerateKey generates a new HMAC key. -func (hkgp *HmacKeyGenParams) GenerateKey( +func (hkgp *HMACKeyGenParams) GenerateKey( extractable bool, keyUsages []CryptoKeyUsage, ) (*CryptoKey, error) { @@ -129,7 +129,7 @@ func (hkgp *HmacKeyGenParams) GenerateKey( key := &CryptoKey{Type: SecretCryptoKeyType, handle: randomKey} // 6. - algorithm := HmacKeyAlgorithm{} + algorithm := HMACKeyAlgorithm{} // 7. algorithm.Name = HMAC @@ -152,11 +152,11 @@ func (hkgp *HmacKeyGenParams) GenerateKey( return key, nil } -// Ensure that HmacKeyGenParams implements the KeyGenerator interface. -var _ KeyGenerator = &HmacKeyGenParams{} +// Ensure that HMACKeyGenParams implements the KeyGenerator interface. +var _ KeyGenerator = &HMACKeyGenParams{} -// HmacKeyAlgorithm represents the algorithm of an HMAC key. -type HmacKeyAlgorithm struct { +// HMACKeyAlgorithm represents the algorithm of an HMAC key. +type HMACKeyAlgorithm struct { KeyAlgorithm // Hash represents the inner hash function to use. @@ -166,7 +166,7 @@ type HmacKeyAlgorithm struct { Length int64 `json:"length"` } -func exportHmacKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { +func exportHMACKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { // 1. if ck.handle == nil { return nil, NewError(OperationError, "key data is not accesible") @@ -189,7 +189,7 @@ func exportHmacKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { } // HashFn returns the hash function to use for the HMAC key. -func (hka *HmacKeyAlgorithm) HashFn() (func() hash.Hash, error) { +func (hka *HMACKeyAlgorithm) HashFn() (func() hash.Hash, error) { hashFn, ok := getHashFn(hka.Hash.Name) if !ok { return nil, NewError(NotSupportedError, fmt.Sprintf("unsupported key hash algorithm %q", hka.Hash.Name)) @@ -198,9 +198,9 @@ func (hka *HmacKeyAlgorithm) HashFn() (func() hash.Hash, error) { return hashFn, nil } -// HmacImportParams represents the object that should be passed as the algorithm parameter +// HMACImportParams represents the object that should be passed as the algorithm parameter // into `SubtleCrypto.GenerateKey`, when generating an HMAC key. -type HmacImportParams struct { +type HMACImportParams struct { Algorithm // Hash represents the name of the digest function to use. You can @@ -216,9 +216,9 @@ type HmacImportParams struct { Length null.Int `json:"length"` } -// newHmacImportParams creates a new HmacImportParams object from the given +// newHMACImportParams creates a new HMACImportParams object from the given // algorithm and params objects. -func newHmacImportParams(rt *goja.Runtime, normalized Algorithm, params goja.Value) (*HmacImportParams, error) { +func newHMACImportParams(rt *goja.Runtime, normalized Algorithm, params goja.Value) (*HMACImportParams, error) { // The specification doesn't explicitly tell us what to do if the // hash field is not present, but we assume it's a mandatory field // and throw an error if it's not present. @@ -250,7 +250,7 @@ func newHmacImportParams(rt *goja.Runtime, normalized Algorithm, params goja.Val length = null.IntFrom(algorithmLengthValue.ToInteger()) } - return &HmacImportParams{ + return &HMACImportParams{ Algorithm: normalized, Hash: normalizedHash, Length: length, @@ -258,7 +258,7 @@ func newHmacImportParams(rt *goja.Runtime, normalized Algorithm, params goja.Val } // ImportKey imports a key from raw key data. It implements the KeyImporter interface. -func (hip *HmacImportParams) ImportKey( +func (hip *HMACImportParams) ImportKey( format KeyFormat, keyData []byte, keyUsages []CryptoKeyUsage, @@ -310,7 +310,7 @@ func (hip *HmacImportParams) ImportKey( } // 9. - algorithm := HmacKeyAlgorithm{} + algorithm := HMACKeyAlgorithm{} // 10. algorithm.Name = HMAC @@ -327,5 +327,5 @@ func (hip *HmacImportParams) ImportKey( return &key, nil } -// Ensure that HmacImportParams implements the KeyImporter interface. -var _ KeyImporter = &HmacImportParams{} +// Ensure that HMACImportParams implements the KeyImporter interface. +var _ KeyImporter = &HMACImportParams{} diff --git a/webcrypto/key.go b/webcrypto/key.go index 685270a..68c1886 100644 --- a/webcrypto/key.go +++ b/webcrypto/key.go @@ -125,7 +125,7 @@ func newKeyGenerator(rt *goja.Runtime, normalized Algorithm, params goja.Value) case AESCbc, AESCtr, AESGcm, AESKw: kg, err = newAESKeyGenParams(rt, normalized, params) case HMAC: - kg, err = newHmacKeyGenParams(rt, normalized, params) + kg, err = newHMACKeyGenParams(rt, normalized, params) } if err != nil { @@ -149,7 +149,7 @@ func newKeyImporter(rt *goja.Runtime, normalized Algorithm, params goja.Value) ( case AESCbc, AESCtr, AESGcm, AESKw: ki = newAESImportParams(normalized) case HMAC: - ki, err = newHmacImportParams(rt, normalized, params) + ki, err = newHMACImportParams(rt, normalized, params) } if err != nil { diff --git a/webcrypto/params.go b/webcrypto/params.go index 3c97778..c1f1d80 100644 --- a/webcrypto/params.go +++ b/webcrypto/params.go @@ -95,17 +95,6 @@ type HMACSignatureParams struct { Name AlgorithmIdentifier } -// HMACImportParams represents the object that should be passed as the -// algorithm parameter into `SubtleCrypto.ImportKey` or `SubtleCrypto.UnwrapKey`, when -// generating a key for the HMAC algorithm. -type HMACImportParams struct { - // Name should be set to AlgorithmKindHmac. - Name AlgorithmIdentifier - - // Hash represents the name of the digest function to use. - Hash AlgorithmIdentifier -} - // PBKDF2Params represents the object that should be passed as the algorithm // parameter into `SubtleCrypto.DeriveKey`, when using the PBKDF2 algorithm. type PBKDF2Params struct { diff --git a/webcrypto/subtle_crypto.go b/webcrypto/subtle_crypto.go index a4356ca..8c7cb0a 100644 --- a/webcrypto/subtle_crypto.go +++ b/webcrypto/subtle_crypto.go @@ -264,7 +264,7 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise { // 10. switch normalized.Name { case HMAC: - keyAlgorithm, ok := ck.Algorithm.(HmacKeyAlgorithm) + keyAlgorithm, ok := ck.Algorithm.(HMACKeyAlgorithm) if !ok { reject(NewError(InvalidAccessError, "key algorithm does not describe a HMAC key")) return @@ -372,7 +372,7 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja switch normalizedAlgorithm.Name { case HMAC: - keyAlgorithm, ok := ck.Algorithm.(HmacKeyAlgorithm) + keyAlgorithm, ok := ck.Algorithm.(HMACKeyAlgorithm) if !ok { reject(NewError(InvalidAccessError, "key algorithm does not describe a HMAC key")) return @@ -744,7 +744,7 @@ func (sc *SubtleCrypto) ExportKey(format KeyFormat, key goja.Value) *goja.Promis return } case HMAC: - result, err = exportHmacKey(ck, format) + result, err = exportHMACKey(ck, format) if err != nil { reject(err) return