{"record":{"id":"94de6f00a63b1bfa","repo":"golang/go","slug":"crypto-sha512-invalid-hash-state-identifier-94de6f","errorCode":null,"errorMessage":"crypto/sha512: invalid hash state identifier","messagePattern":"crypto/sha512: invalid hash state identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/sha512/sha512.go","lineNumber":166,"sourceCode":"\t\tpanic(\"unknown size\")\n\t}\n\tb = byteorder.BEAppendUint64(b, d.h[0])\n\tb = byteorder.BEAppendUint64(b, d.h[1])\n\tb = byteorder.BEAppendUint64(b, d.h[2])\n\tb = byteorder.BEAppendUint64(b, d.h[3])\n\tb = byteorder.BEAppendUint64(b, d.h[4])\n\tb = byteorder.BEAppendUint64(b, d.h[5])\n\tb = byteorder.BEAppendUint64(b, d.h[6])\n\tb = byteorder.BEAppendUint64(b, d.h[7])\n\tb = append(b, d.x[:d.nx]...)\n\tb = append(b, make([]byte, len(d.x)-d.nx)...)\n\tb = byteorder.BEAppendUint64(b, d.len)\n\treturn b, nil\n}\n\nfunc (d *Digest) UnmarshalBinary(b []byte) error {\n\tif len(b) < len(magic512) {\n\t\treturn errors.New(\"crypto/sha512: invalid hash state identifier\")\n\t}\n\tswitch {\n\tcase d.size == size384 && string(b[:len(magic384)]) == magic384:\n\tcase d.size == size224 && string(b[:len(magic512_224)]) == magic512_224:\n\tcase d.size == size256 && string(b[:len(magic512_256)]) == magic512_256:\n\tcase d.size == size512 && string(b[:len(magic512)]) == magic512:\n\tdefault:\n\t\treturn errors.New(\"crypto/sha512: invalid hash state identifier\")\n\t}\n\tif len(b) != marshaledSize {\n\t\treturn errors.New(\"crypto/sha512: invalid hash state size\")\n\t}\n\tb = b[len(magic512):]\n\tb, d.h[0] = consumeUint64(b)\n\tb, d.h[1] = consumeUint64(b)\n\tb, d.h[2] = consumeUint64(b)\n\tb, d.h[3] = consumeUint64(b)\n\tb, d.h[4] = consumeUint64(b)","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/sha512/sha512.go#L148-L184","documentation":"Returned by sha512 Digest.UnmarshalBinary when len(b) < len(magic512): the input is too short to even contain the magic prefix that identifies the SHA-512 family variant. This is the initial length gate before the variant-specific magic switch runs.","triggerScenarios":"Calling UnmarshalBinary with an empty slice, a very short slice, or any buffer shorter than the magic prefix length. The switch on d.size (384/224/256/512) is never reached because the input cannot physically hold a magic.","commonSituations":"Empty/nil buffer returned from a failed read; a framing bug that delivered only the length prefix; persisted state column truncated to near-zero length; calling UnmarshalBinary on bytes that were never actually marshaled (e.g. raw digest output rather than marshaled state).","solutions":["Check len(b) >= len(magic512) (the longest magic in the family) before calling, and discard anything shorter.","Distinguish 'marshaled state' from 'final digest output' — UnmarshalBinary expects the former, not the latter.","Verify the read/persistence layer returned the full expected byte count before invoking UnmarshalBinary."],"exampleFix":"// before\nd.UnmarshalBinary(nilOrShort) // < magic prefix -> error\n// after\nif len(b) < marshaledSize { return errors.New(\"state too short\") }\nd.UnmarshalBinary(b)","handlingStrategy":"validation","validationCode":"if len(b) < len(magic512) {\n    return fmt.Errorf(\"sha512 state too short: got %d want >= %d\", len(b), len(magic512))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Distinguish 'marshaled state' from 'final digest output'; UnmarshalBinary wants the former.","Verify the read returned at least the magic prefix length before calling.","Treat empty/short buffers as corrupt rather than passing them in."],"tags":["crypto","hash","sha512","serialization","go-stdlib"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}