in3rsha/sha256-animation · error

Invalid binary string: #{input}

Error message

Invalid binary string: #{input}

What it means

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.

Source

Thrown at sha256lib.rb:77

      sleep 1.0 * multiplier
    else
      sleep speed
    end
  end
end

# Detect input type base on prefix (i.e. binary, hex, or otherwise just a string)
def input_type(input)
  # Check if input is referencing a file
  if(File.file?(input))
  	return "file"
  else
	  # Check for hex or binary prefix
	  case input[0..1]
	  when "0b"
		# check it's a valid binary string
		if input[2..-1] =~ /[^0-1]/ # only 1s and 0s
		  puts "Invalid binary string: #{input}"
		  exit
		end
		return "binary"
	  when "0x"
		# check it's a valid hex string
		if input[2..-1] !~ /^[0-9A-F]+$/i # only hex chars (case-insensitive)
		  puts "Invalid hex string: #{input}"
		  exit
		end
		return "hex"
	  else
		return "string"
	  end
  end
end

# Convert input (hex, ascii) to array of bytes
def bytes(input, type)

View on GitHub (pinned to 871e976d69)

Solutions

  1. 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)
  2. If the value is hexadecimal, use the 0x prefix instead — hex payloads accept 0-9 and a-f of any case
  3. 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
  4. 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

Example fix

# before
ruby sha256.rb '0b1012'    # -> Invalid binary string: 0b1012
ruby sha256.rb '0b1010 1010' # -> Invalid binary string: 0b1010 1010

# after
ruby sha256.rb '0b10110100'  # 8 clean bits, hashed as binary
Defensive patterns

Strategy: validation

Validate before calling

# run before input_type() or the sha256.rb CLI
bad_bits = input.to_s.start_with?('0b') && input[2..].to_s.match(/[^0-1]/)
abort 'payload after 0b must be only 0s and 1s' if bad_bits
type = input_type(input)

Type guard

# returns :binary, :hex, :file, :string, or nil when sha256lib would exit
def classify(input)
  return :file if File.file?(input)
  case input[0..1]
  when '0b' then input[2..].to_s.empty? || input[2..].match(/[^0-1]/) ? nil : :binary
  when '0x' then input[2..].to_s.empty? || input[2..].match(/[^0-9a-fA-F]/) ? nil : :hex
  else :string
  end
end

raise ArgumentError, 'input_type would exit' if classify(input).nil?

Try / catch

# exit() raises SystemExit, so an in-process caller can intercept the stop:
begin
  type = input_type(candidate)
rescue SystemExit
  warn 'sha256lib rejected the binary input (its message already went to stdout)'
  type = nil
end

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of in3rsha/sha256-animation@871e976d69 (2026-08-23). Data as JSON: /api/errors/dc2c12d7e392d0ab. Report an issue: GitHub.