{"record":{"id":"5ae7a7c3e92b722b","repo":"golang/go","slug":"crypto-sha1-invalid-hash-state-identifier-5ae7a7","errorCode":null,"errorMessage":"crypto/sha1: invalid hash state identifier","messagePattern":"crypto/sha1: invalid hash state identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/sha1/sha1.go","lineNumber":71,"sourceCode":"\treturn d.AppendBinary(make([]byte, 0, marshaledSize))\n}\n\nfunc (d *digest) AppendBinary(b []byte) ([]byte, error) {\n\tb = append(b, magic...)\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 = 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(magic) || string(b[:len(magic)]) != magic {\n\t\treturn errors.New(\"crypto/sha1: invalid hash state identifier\")\n\t}\n\tif len(b) != marshaledSize {\n\t\treturn errors.New(\"crypto/sha1: invalid hash state size\")\n\t}\n\tb = b[len(magic):]\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 = b[copy(d.x[:], b):]\n\tb, d.len = consumeUint64(b)\n\td.nx = int(d.len % chunk)\n\treturn nil\n}\n\nfunc consumeUint64(b []byte) ([]byte, uint64) {\n\treturn b[8:], byteorder.BEUint64(b)","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/sha1/sha1.go#L53-L89","documentation":"Thrown by sha1.digest.UnmarshalBinary when the input does not begin with the sha1 magic identifier. UnmarshalBinary reverses MarshalBinary to restore a hash state; the magic prefix distinguishes a SHA-1 blob from other hash encodings. A missing or wrong prefix means the bytes are not a valid SHA-1 state.","triggerScenarios":"Calling UnmarshalBinary (directly, or via encoding/gob, encoding/json with MarshalBinary, or hash.Hash reset/restore) on []byte that is not a SHA-1-marshaled state. Common when the wrong hash's binary blob is fed to a SHA-1 digest.","commonSituations":"Interchanging marshaled states between SHA-256 and SHA-1; corrupted gob stream storing hash state; truncated blob; passing arbitrary user data to UnmarshalBinary.","solutions":["Ensure the bytes passed to UnmarshalBinary came from a SHA-1 MarshalBinary call.","Check len(b) >= len(magic) and the magic prefix before calling, returning your own error.","Use a single hash algorithm end-to-end so states are type-matched.","If the source is untrusted, treat UnmarshalBinary failure as expected and discard the input."],"exampleFix":"// before\nvar d sha1.digest\nd.UnmarshalBinary(sha256state) // wrong algorithm's blob\n\n// after\nif !bytes.HasPrefix(b, []byte(\"sha\\x01\")) { // sha1 magic\n    return errors.New(\"not a sha1 state\")\n}\nerr := d.UnmarshalBinary(b)","handlingStrategy":"validation","validationCode":"// sha1 magic is \"sha\\x01\" (3 bytes). Verify prefix before UnmarshalBinary.\nvar sha1Magic = []byte{'s', 'h', 'a', 0x01} // confirm exact bytes from source\n\nfunc safeUnmarshal(d *sha1.Digest, b []byte) error {\n    if len(b) < len(sha1Magic) || !bytes.Equal(b[:len(sha1Magic)], sha1Magic) {\n        return errors.New(\"not a sha1 marshaled state\")\n    }\n    return d.UnmarshalBinary(b)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Marshal and unmarshal with the same hash package; do not mix algorithm blobs.","Version-stamp persisted hash states so a mismatch is detectable up front.","Treat UnmarshalBinary as a parser of untrusted input and handle its error.","Keep gob/json streams that carry hash states on a single hash algorithm."],"tags":["crypto","sha1","serialization","go"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}