{"record":{"id":"bea8763a02cd87d4","repo":"golang/go","slug":"crypto-sha256-invalid-hash-state-identifier-bea876","errorCode":null,"errorMessage":"crypto/sha256: invalid hash state identifier","messagePattern":"crypto/sha256: invalid hash state identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/sha256/sha256.go","lineNumber":91,"sourceCode":"\t\tb = append(b, magic256...)\n\t}\n\tb = byteorder.BEAppendUint32(b, d.h[0])\n\tb = byteorder.BEAppendUint32(b, d.h[1])\n\tb = byteorder.BEAppendUint32(b, d.h[2])\n\tb = byteorder.BEAppendUint32(b, d.h[3])\n\tb = byteorder.BEAppendUint32(b, d.h[4])\n\tb = byteorder.BEAppendUint32(b, d.h[5])\n\tb = byteorder.BEAppendUint32(b, d.h[6])\n\tb = byteorder.BEAppendUint32(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(magic224) || (d.is224 && string(b[:len(magic224)]) != magic224) || (!d.is224 && string(b[:len(magic256)]) != magic256) {\n\t\treturn errors.New(\"crypto/sha256: invalid hash state identifier\")\n\t}\n\tif len(b) != marshaledSize {\n\t\treturn errors.New(\"crypto/sha256: invalid hash state size\")\n\t}\n\tb = b[len(magic224):]\n\tb, d.h[0] = consumeUint32(b)\n\tb, d.h[1] = consumeUint32(b)\n\tb, d.h[2] = consumeUint32(b)\n\tb, d.h[3] = consumeUint32(b)\n\tb, d.h[4] = consumeUint32(b)\n\tb, d.h[5] = consumeUint32(b)\n\tb, d.h[6] = consumeUint32(b)\n\tb, d.h[7] = consumeUint32(b)\n\tb = b[copy(d.x[:], b):]\n\tb, d.len = consumeUint64(b)\n\td.nx = int(d.len % chunk)\n\treturn nil\n}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/sha256/sha256.go#L73-L109","documentation":"Returned by sha256 Digest.UnmarshalBinary when the input is shorter than the magic prefix or the prefix does not equal magic224 ('sha\\x03') for a SHA-224 digest or magic256 ('sha\\x04') for a SHA-256 digest. The magic identifies which variant produced the marshaled state; a mismatch means the bytes are not a valid SHA-256/224 serialized state (or are the wrong variant).","triggerScenarios":"Calling UnmarshalBinary on a SHA-256 digest with bytes from SHA-224 (or vice versa), bytes from a different hash, corrupted bytes, or a truncated buffer that drops the magic prefix entirely.","commonSituations":"Persisting hash state to a database column and loading it back with the wrong schema version; sending hash state over a protocol where another hash family writes the same field; calling UnmarshalBinary on a SHA-256 digest for state that was marshaled before a code change switched the algorithm to SHA-224.","solutions":["Verify the marshaled bytes came from the exact same digest variant (SHA-256 vs SHA-224) on the producing side.","If migrating variants, re-hash the original input from scratch rather than trying to cross-unmarshal.","Store the algorithm name alongside the marshaled state and dispatch UnmarshalBinary to a digest of the matching type.","Check len(b) >= len(magic) before calling and treat anything shorter as corrupt."],"exampleFix":"// before\nvar d sha256.Digest (224 variant)\nd.UnmarshalBinary(stateFromSHA256) // wrong magic\n// after\nvar d sha256.Digest (256 variant matching producer)\nd.UnmarshalBinary(stateFromSHA256)","handlingStrategy":"validation","validationCode":"const magic256 = \"sha\\x04\"\nif len(b) < len(magic256) || string(b[:len(magic256)]) != magic256 {\n    return errors.New(\"not SHA-256 marshaled state\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Tag persisted hash state with the algorithm name and verify the tag before UnmarshalBinary.","Always pair a producer's MarshalBinary with the matching variant's UnmarshalBinary.","Re-hash from the original input when the variant changes rather than cross-unmarshaling."],"tags":["crypto","hash","sha256","serialization","go-stdlib"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}