forked from in3rsha/sha256-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.rb
73 lines (64 loc) · 1.65 KB
/
final.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
# coding: utf-8
require_relative "sha256lib.rb"
# -------
# Default
# -------
if !defined? $input
# default
$input = "abc"
# argument passed
$input = ARGV[0] if ARGV[0] # only accept strings for now
$message = $input.unpack("B*")[0] # 011000010110001001100011
$padded = padding($message)
$blocks = split($padded, 512)
# Set initial hash state using initial hash values
$hash = IV
# For each message block
$blocks.each do |block|
# Prepare 64 word message schedule
schedule = calculate_schedule(block)
# Remember starting hash values
initial = $hash.clone
# Apply compression function to update hash values
$hash = compression(initial, schedule, constants = K)
end
end
# Start
system "clear"
puts $state + "\n" if defined? $state
puts "----------------"
puts "final hash value: (H#{$blocks.size})"
puts "----------------"
registers = ("a".."h").to_a
8.times do |i|
puts "#{registers[i]} = #{bits($hash[i])}"
end
delay(:slowest)
# Frame - Hexadecimal
system "clear"
puts $state + "\n" if defined? $state
puts "----------------"
puts "final hash value: (H#{$blocks.size})"
puts "----------------"
8.times do |i|
puts "#{registers[i]} = #{bits($hash[i])} = #{hex($hash[i])}"
end
delay(:slowest)
# Frame - Concatenate
$digest = ""
8.times do |i|
$digest << hex($hash[i])
system "clear"
puts $state + "\n" if defined? $state
puts "----------------"
puts "final hash value: (H#{$blocks.size})"
puts "----------------"
8.times do |j|
puts "#{registers[j]} = #{bits($hash[j])} = #{hex($hash[j])}"
end
puts
puts "#{$digest}"
delay(:fastest)
end
delay(:end)
delay(:end) # extra delay for the absolute final frame