mirror of
https://github.com/in3rsha/sha256-animation
synced 2024-05-03 22:26:13 +02:00
Added ability to pass a file as input
This commit is contained in:
parent
9f431b581f
commit
a5700359d4
|
@ -11,11 +11,16 @@ if !defined? $input
|
|||
# Detect input type (binary, hex, or string)
|
||||
$type = input_type($input)
|
||||
|
||||
# Read file if file is given
|
||||
if $type == "file"
|
||||
$input = File.read($input)
|
||||
end
|
||||
|
||||
# Convert input to bytes (if possible)
|
||||
$bytes = bytes($input, $type)
|
||||
|
||||
# Set message (binary representation of data)
|
||||
if $type == "string" || $type == "hex"
|
||||
if $type == "string" || $type == "file" || $type == "hex"
|
||||
$message = $bytes.map { |x| x.to_s(2).rjust(8, "0") }.join # convert bytes to binary string
|
||||
end
|
||||
if $type == "binary"
|
||||
|
|
39
sha256lib.rb
39
sha256lib.rb
|
@ -65,24 +65,29 @@ end
|
|||
|
||||
# Detect input type base on prefix (i.e. binary, hex, or otherwise just a string)
|
||||
def input_type(input)
|
||||
# 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"
|
||||
# Check if input is referencing a file
|
||||
if(File.file?(input))
|
||||
return "file"
|
||||
else
|
||||
return "string"
|
||||
# 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
|
||||
|
||||
|
|
Loading…
Reference in New Issue