grafana/k6 · error

failed to extract algorithm

Error message

failed to extract algorithm: %w

What it means

Returned by exportSymmetricJWK (exporting an HMAC or AES key as JWK) when the internal extractAlg helper fails to derive the JWK `alg` string from the key's algorithm object. The wrapped error typically comes from the algorithm name being too short to strip its prefix, meaning the CryptoKey holds a malformed or non-standard algorithm representation — usually a sign the key was created/imported with inconsistent algorithm parameters.

Solutions

  1. Re-import or re-generate the key using a standard algorithm name (e.g. 'AES-GCM', 'HMAC', 'AES-CBC')
  2. Inspect the wrapped error message to see which algorithm component was rejected
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/webcrypto/jwk.go:85 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/34b27e87bbcafe3e. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/jwk.go:86

// exportSymmetricJWK exports a symmetric key as a map of JWK key parameters.
func exportSymmetricJWK(key *CryptoKey) (*JsonWebKey, error) {
	rawKey, ok := key.handle.([]byte)
	if !ok {
		return nil, errors.New("key's handle isn't a byte slice")
	}

	// wrap result into the object that is expected to be returned
	exported := &JsonWebKey{}

	exported.Set("k", base64URLEncode(rawKey))
	exported.Set("kty", JWKOctKeyType)
	exported.Set("ext", key.Extractable)
	exported.Set("key_ops", key.Usages)

	algV, err := extractAlg(key.Algorithm, len(rawKey))
	if err != nil {
		return nil, fmt.Errorf("failed to extract algorithm: %w", err)
	}
	exported.Set("alg", algV)

	return exported, nil
}

func extractAlg(inAlg any, keyLen int) (string, error) {
	switch alg := inAlg.(type) {
	case hasHash:
		v := alg.hash()
		if len(v) < 4 {
			return "", errors.New("length of hash algorithm is less than 4: " + v)
		}
		return "HS" + v[4:], nil
	case hasAlg:
		v := alg.alg()
		if len(v) < 4 {
			return "", errors.New("length of named algorithm is less than 4: " + v)

View on GitHub (pinned to 8d06114777)