Skip to content

Commit

Permalink
Rename the HMAC-related structures following the Go naming conventions
Browse files Browse the repository at this point in the history
Instead of WebCrypto's
  • Loading branch information
oleiade committed Apr 14, 2023
1 parent 8a56dce commit c8c80aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
44 changes: 22 additions & 22 deletions webcrypto/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -71,15 +71,15 @@ 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,
}, nil
}

// GenerateKey generates a new HMAC key.
func (hkgp *HmacKeyGenParams) GenerateKey(
func (hkgp *HMACKeyGenParams) GenerateKey(
extractable bool,
keyUsages []CryptoKeyUsage,
) (*CryptoKey, error) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func (hkgp *HmacKeyGenParams) GenerateKey(
key := &CryptoKey{Type: SecretCryptoKeyType, handle: randomKey}

// 6.
algorithm := HmacKeyAlgorithm{}
algorithm := HMACKeyAlgorithm{}

// 7.
algorithm.Name = HMAC
Expand All @@ -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.
Expand All @@ -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")
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -250,15 +250,15 @@ 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,
}, nil
}

// 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,
Expand Down Expand Up @@ -310,7 +310,7 @@ func (hip *HmacImportParams) ImportKey(
}

// 9.
algorithm := HmacKeyAlgorithm{}
algorithm := HMACKeyAlgorithm{}

// 10.
algorithm.Name = HMAC
Expand All @@ -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{}
4 changes: 2 additions & 2 deletions webcrypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
11 changes: 0 additions & 11 deletions webcrypto/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c8c80aa

Please sign in to comment.