{"record":{"id":"dc2c12d7e392d0ab","repo":"in3rsha/sha256-animation","slug":"invalid-binary-string-input","errorCode":null,"errorMessage":"Invalid binary string: #{input}","messagePattern":"Invalid binary string: #(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sha256lib.rb","lineNumber":77,"sourceCode":"      sleep 1.0 * multiplier\n    else\n      sleep speed\n    end\n  end\nend\n\n# Detect input type base on prefix (i.e. binary, hex, or otherwise just a string)\ndef input_type(input)\n  # Check if input is referencing a file\n  if(File.file?(input))\n  \treturn \"file\"\n  else\n\t  # Check for hex or binary prefix\n\t  case input[0..1]\n\t  when \"0b\"\n\t\t# check it's a valid binary string\n\t\tif input[2..-1] =~ /[^0-1]/ # only 1s and 0s\n\t\t  puts \"Invalid binary string: #{input}\"\n\t\t  exit\n\t\tend\n\t\treturn \"binary\"\n\t  when \"0x\"\n\t\t# check it's a valid hex string\n\t\tif input[2..-1] !~ /^[0-9A-F]+$/i # only hex chars (case-insensitive)\n\t\t  puts \"Invalid hex string: #{input}\"\n\t\t  exit\n\t\tend\n\t\treturn \"hex\"\n\t  else\n\t\treturn \"string\"\n\t  end\n  end\nend\n\n# Convert input (hex, ascii) to array of bytes\ndef bytes(input, type)","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/in3rsha/sha256-animation/blob/871e976d69e66474ef5239ee82031b7d87cbcafc/sha256lib.rb#L59-L95","documentation":"The helper input_type() in sha256lib.rb classifies each CLI argument by prefix: 0b means binary, 0x means hex, an existing file path means file, anything else is a plain string. If an argument starts with 0b but the remainder contains any character other than 0 or 1 (checked with input[2..-1] =~ /[^0-1]/), the script prints this message and calls exit — a hard stop, not a Ruby exception. The guard exists because the later byte conversion can only interpret bits. Note the misclassification risk: ASCII text that coincidentally begins with the two characters 0b is sent down this same branch.","triggerScenarios":"Running ruby sha256.rb 0b1012 (typo digit 2), ruby sha256.rb '0b1010 1010' (space inside the bits), or calling input_type('0b1111_0000') from code (underscore grouping). Also fires when you meant to hash plain ASCII text that begins with 0b, because only existing file paths (File.file? is checked first) dodge the prefix sniffing.","commonSituations":"Hand-transcribing bit strings from SHA-256 tutorials and mistyping a digit; pasting binary that carries spaces, underscores or commas from formatted text; shell scripts passing a variable with stray whitespace; intending ASCII text or a path that begins with 0b, which the prefix detector cannot express.","solutions":["Strip everything except 0 and 1 from the payload after the 0b prefix — spaces, underscores and commas are the usual contaminants (0b1010 1010 becomes 0b10101010)","If the value is hexadecimal, use the 0x prefix instead — hex payloads accept 0-9 and a-f of any case","If you meant the literal ASCII text that starts with 0b, remove or change those leading characters, or put the text in a file and pass its path — the file branch is checked before prefixes","Pre-validate in your own wrapper (reject any 0b input whose remainder fails a /[^0-1]/ scan) so you raise your own exception instead of hitting the built-in puts-plus-exit"],"exampleFix":"# before\nruby sha256.rb '0b1012'    # -> Invalid binary string: 0b1012\nruby sha256.rb '0b1010 1010' # -> Invalid binary string: 0b1010 1010\n\n# after\nruby sha256.rb '0b10110100'  # 8 clean bits, hashed as binary","handlingStrategy":"validation","validationCode":"# run before input_type() or the sha256.rb CLI\nbad_bits = input.to_s.start_with?('0b') && input[2..].to_s.match(/[^0-1]/)\nabort 'payload after 0b must be only 0s and 1s' if bad_bits\ntype = input_type(input)","typeGuard":"# returns :binary, :hex, :file, :string, or nil when sha256lib would exit\ndef classify(input)\n  return :file if File.file?(input)\n  case input[0..1]\n  when '0b' then input[2..].to_s.empty? || input[2..].match(/[^0-1]/) ? nil : :binary\n  when '0x' then input[2..].to_s.empty? || input[2..].match(/[^0-9a-fA-F]/) ? nil : :hex\n  else :string\n  end\nend\n\nraise ArgumentError, 'input_type would exit' if classify(input).nil?","tryCatchPattern":"# exit() raises SystemExit, so an in-process caller can intercept the stop:\nbegin\n  type = input_type(candidate)\nrescue SystemExit\n  warn 'sha256lib rejected the binary input (its message already went to stdout)'\n  type = nil\nend","preventionTips":["Keep 0b payloads strictly 0s and 1s — strip spaces, underscores and commas before hashing","Never feed ASCII text that starts with the characters 0b to these scripts; there is no escape syntax","Remember the file branch wins: an existing path starting 0b or 0x is read as a file, not parsed as a literal","Wrap input_type() in your own validated entry point rather than calling it raw in longer scripts"],"tags":["ruby","sha256","input-validation","binary","cli"],"backgroundTag":"invalid-binary-literal","analyzedSha":"871e976d69e66474ef5239ee82031b7d87cbcafc","analyzedAt":"2026-08-23T12:17:23.862Z","schemaVersion":2},"datasetVersion":"2026-08-23T16:17:53.355Z"}