Skip to content

Bug: HMAC.Algorithm.fromNative has a duplicate dictionary key, crashes on first access #83

Description

@alexhunsley

HMAC.swift's fromNative lookup table maps native CCHmacAlgorithm constants back to HMAC.Algorithm cases:

static let fromNative: [CCHmacAlgorithm: Algorithm] = [
    CCHmacAlgorithm(kCCHmacAlgSHA1): .sha1,
    CCHmacAlgorithm(kCCHmacAlgSHA1): .md5,   // should be kCCHmacAlgMD5
    ...
]

kCCHmacAlgSHA1 is used as the key for both .sha1 and .md5 — the .md5 entry should use kCCHmacAlgMD5. Since Swift dictionary
literals fatal-error on duplicate keys, any access to fromNative (e.g. via fromNativeValue(nativeAlg:)) traps at runtime with
Fatal error: Dictionary literal contains duplicate keys.

Both members are internal, so this isn't reachable by consumers of the library today, and there's no existing test coverage for
fromNativeValue at all — likely why this has gone unnoticed.

Fix: use kCCHmacAlgMD5 for the .md5 entry, plus a regression test that round-trips every Algorithm case through nativeValue() →
fromNativeValue().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions