-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtarget.rb
More file actions
61 lines (35 loc) · 1.46 KB
/
target.rb
File metadata and controls
61 lines (35 loc) · 1.46 KB
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
###
# type to run:
# $ ruby ./target.rb
require 'digest'
def sha256( msg )
Digest::SHA256.hexdigest( msg )
end
p hash = sha256( "Hello, world!0" )
#=> "1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64"
p num = hash.to_i( 16 ) ## convert hex(adecimal) hash to (integer) number
#=> 8626955810696577806643191367156697543225924734479747394789354329720975740004
p log2 = Math.log2( num )
#=> 252.2534586827243
#########
# Let's try the winning lucky number example:
# "Hello, world!4250" => 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9 = 2^239.61238653
p hash = sha256( "Hello, world!4250" )
#=> "0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9"
p num = hash.to_i( 16 )
#=> 1350565582647790482127632554504241516291697500941742491868079705537959145
p log2 = Math.log2( num )
#=> 239.61238652983653
puts "%0256b" % num
#=> 0000000000000000110000111010111101000010111111000011000100010000
# 0011111100011111110111000000000101010001111110100111010001111111
# 1111100001110011010010011010010001110001010011011111011111001100
# 0101001011101010010001100100111000010010110111001101010011101001
####################
# The maximum for a 256-bit number is - surprise, surprise - 256
p max = "f"*64
#=> "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
p num = max.to_i( 16 )
#=> 115792089237316195423570985008687907853269984665640564039457584007913129639935
p log2 = Math.log2( num )
#=> 256.0