{"record":{"id":"7d2758e8a54cbd71","repo":"OpenNHP/opennhp","slug":"unsupported-hash-type-d","errorCode":null,"errorMessage":"unsupported hash type: %d","messagePattern":"unsupported hash type: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/crypto.go","lineNumber":101,"sourceCode":"}\n\nfunc NewHash(t HashTypeEnum) (hash.Hash, error) {\n\tswitch t {\n\tcase HASH_BLAKE2S:\n\t\th, err := blake2s.New256(nil)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create blake2s hash: %w\", err)\n\t\t}\n\t\treturn h, nil\n\n\tcase HASH_SM3:\n\t\treturn sm3.New(), nil\n\n\tcase HASH_SHA256:\n\t\treturn sha256.New(), nil\n\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported hash type: %d\", t)\n\t}\n}\n\ntype Ecdh interface {\n\tSetPrivateKey(prk []byte) error\n\tPrivateKey() []byte\n\tPublicKey() []byte\n\tSharedSecret(pbk []byte) []byte\n\tName() string\n\tPrivateKeyBase64() string\n\tPublicKeyBase64() string\n\tIdentity() []byte\n\tMidPublicKey() []byte\n}\n\nfunc ECDHFromKey(t EccTypeEnum, prk []byte) (e Ecdh) {\n\tswitch t {\n\tcase ECC_CURVE25519:","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/crypto.go#L83-L119","documentation":"NewHash returns this error when passed a HashTypeEnum value that is not HASH_BLAKE2S (0), HASH_SM3 (1), or HASH_SHA256 (2). HashTypeEnum is an int-based enum, so any out-of-range or garbage integer (e.g. an unvalidated value read from config, wire data, or an uninitialized variable) falls into the default branch. It indicates an invalid algorithm selector reaching the crypto layer.","triggerScenarios":"Calling NewHash(t) with t cast from an int outside 0-2: an uninitialized HashTypeEnum set to a sentinel like -1, a value decoded from a config file or network message, or an enum extended by another package without updating NewHash's switch.","commonSituations":"Hand-editing config.toml or code that maps a cipher-scheme number to a hash type incorrectly; deserializing a peer's algorithm identifier that doesn't match the local enum; adding a new hash algorithm to the enum but forgetting the switch case in crypto.go.","solutions":["Only construct hash types via NewCipherSuite(scheme), which always returns a valid HashType (HASH_SM3 or HASH_BLAKE2S)","Validate any externally-sourced integer against the known constants before casting to HashTypeEnum","Log the offending numeric value (%d) and compare it against the HashTypeEnum constants 0/1/2","If a new hash constant was added, add the corresponding case to NewHash's switch and rebuild"],"exampleFix":"// before\nh, _ := core.NewHash(core.HashTypeEnum(cfg.HashCode))\n// after\nif cfg.HashCode < int(core.HASH_BLAKE2S) || cfg.HashCode > int(core.HASH_SHA256) {\n    return fmt.Errorf(\"invalid hash code %d in config\", cfg.HashCode)\n}\nh, err := core.NewHash(core.HashTypeEnum(cfg.HashCode))\nif err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"func isValidHashType(t core.HashTypeEnum) bool {\n    return t >= core.HASH_BLAKE2S && t <= core.HASH_SHA256\n}\n\nif !isValidHashType(t) {\n    return fmt.Errorf(\"hash type %d out of range\", int(t))\n}","typeGuard":"func asHashType(v int) (core.HashTypeEnum, bool) {\n    if v < int(core.HASH_BLAKE2S) || v > int(core.HASH_SHA256) {\n        return 0, false\n    }\n    return core.HashTypeEnum(v), true\n}","tryCatchPattern":"h, err := core.NewHash(t)\nif err != nil {\n    if strings.Contains(err.Error(), \"unsupported hash type\") {\n        return fmt.Errorf(\"bad algorithm selector %d: %w\", int(t), err)\n    }\n    return err\n}","preventionTips":["Derive hash types only from NewCipherSuite(common.CIPHER_SCHEME_*)","Never cast raw config/protocol integers to HashTypeEnum without range checks","Update NewHash's switch immediately when extending HashTypeEnum"],"tags":["crypto","hash","enum","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}