forked from in3rsha/sha256-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotr.rb
40 lines (35 loc) · 747 Bytes
/
rotr.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
# -----------
# Shift Right
# -----------
def rotr(n, x)
mask = 2**32 - 1
right = (x >> n) & mask
left = (x << 32-n) & mask
result = right | left
return result
end
# -----
# Input
# -----
# defaults
x = 0b11111111000000001111111100000000 #0b11101001101101011101101110100101
n = 32
# arguments passed
x = ARGV[0].to_i(2) if ARGV[0] # binary
n = ARGV[1].to_i if ARGV[1] # integer
# check arguments
if ARGV[0] && ARGV[0].size > 32
puts "We only operate on 32-bit words in SHA-256. Your x is #{ARGV[0].size} bits."; exit
end
# ---------
# Animation
# ---------
s = n.to_s.ljust(2, " ")
n.times do |i|
system "clear"
i += 1
puts " x: #{"%032b" % x}"
puts "ROTR #{s}: #{"%032b" % rotr(i, x)}"
sleep 0.10
end
sleep 0.5