-
Notifications
You must be signed in to change notification settings - Fork 165
/
padding.rb
95 lines (83 loc) · 2.19 KB
/
padding.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require_relative "sha256lib.rb"
# -------
# Default
# -------
if !defined? $input
# default
$input = "abc"
$message = $input.unpack("B*")[0] # 011000010110001001100011
# argument passed
$message = ARGV[0] if ARGV[0] # accept binary message string
end
# -------
# Padding
# -------
# pad the message so that it's a multiple of 512 bits
$l = $message.size # length of message
$k = (448 - $l - 1) % 512 # pad with zeros up to 448 bits (64 short of 512 bits)
# message padding size M
# -------------------------------------------------------------
# | M |1|...00000000000000000000|...0000001100|
# -------------------------------------------------------------
# <----------------- 448 bits ----------------> <- 64 bits ->
$padded = $message + "1" + ("0" * $k) + $l.to_s(2).rjust(64, "0")
# ---------
# Animation
# ---------
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding:"
puts "-------"
delay(:normal)
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding: (#{$message.size} bits)"
puts "-------"
puts "message: #{$message}"
delay(:normal)
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding: (#{($message + "1").size} bits)"
puts "-------"
puts "message: #{$message}1"
delay(:normal)
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding: (#{($message + "1").size} bits -> #{($message + "1").size + $k} bits)"
puts "-------"
print "message: #{$message}1"
if $delay == "enter" || $delay == "nodelay" # show all padded zeros in one go if we're stepping through with keyboard
puts "0" * $k
else
$k.times do |i|
print "0"
sleep 0.005
end
end
delay(:normal)
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding: (#{($message + "1").size + $k} bits)"
puts "-------"
print "message: #{$message}1#{"0" * $k}"
delay(:normal)
system "clear"
puts $state + "\n" if defined? $state
puts "-------"
puts "padding: (#{$padded.size} bits)"
puts "-------"
puts "message: #{$padded}"
delay(:end)
# Save Final State
$state = <<-FRAME
#{$state}
-------
padding: (#{$padded.size} bits)
-------
message: #{$padded}
FRAME