stribog 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/stribog.rb +0 -1
- data/lib/stribog/binary_vector.rb +34 -16
- data/lib/stribog/compression.rb +2 -2
- data/lib/stribog/create_hash.rb +82 -52
- data/lib/stribog/version.rb +1 -1
- metadata +2 -3
- data/lib/stribog/message.rb +0 -72
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56a62cd1418c7e1264f036de4784f516f51a2ee1
|
4
|
+
data.tar.gz: ab86cf5faf9d8a5c214a5f88bff9f9128b7da8ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53203b0d1059510c0737e508cfd9bb9da92d12831559605ee811f8ef6e687121df7a3983ace8e3f079f2fda1243bf8fce680e211725eb7b862832374a0770b93
|
7
|
+
data.tar.gz: 35974a63681b24a92cf62e3095d136138eec89e3ebbae94539df53c4f95c7bf4e564ff323df0fa1042c7fe7e3422d98fccb1b6d73f3032b5ce85de71ee5f9a30
|
data/.gitignore
CHANGED
data/lib/stribog.rb
CHANGED
@@ -5,15 +5,28 @@ module Stribog
|
|
5
5
|
class BinaryVector
|
6
6
|
include Enumerable
|
7
7
|
|
8
|
+
attr_reader :vector
|
9
|
+
|
8
10
|
class << self
|
9
11
|
def from_byte(byte, size: 8)
|
10
|
-
new(byte.to_s(2).chars.map(&:to_i)).
|
12
|
+
new(byte.to_s(2).chars.map(&:to_i)).addition_by_zeros(size: size)
|
11
13
|
end
|
12
14
|
|
13
15
|
def from_byte_array(byte_array, size: 8)
|
14
16
|
new(byte_array.map { |byte| from_byte(byte, size: size).to_a }
|
15
17
|
.inject([]) { |acc, elem| acc + elem })
|
16
18
|
end
|
19
|
+
|
20
|
+
def from_hex(hex)
|
21
|
+
new hex_to_bin(hex)
|
22
|
+
end
|
23
|
+
|
24
|
+
def hex_to_bin(hex)
|
25
|
+
hex.chars.map do |x|
|
26
|
+
bin = x.to_i(16).to_s(2)
|
27
|
+
'0' * (4 - bin.length) + bin
|
28
|
+
end.join.chars.map(&:to_i)
|
29
|
+
end
|
17
30
|
end
|
18
31
|
|
19
32
|
def initialize(vector)
|
@@ -23,16 +36,21 @@ module Stribog
|
|
23
36
|
|
24
37
|
def ^(other)
|
25
38
|
raise 'DimensionError' unless according_dimension?(other)
|
26
|
-
self.class.new
|
39
|
+
self.class.new vector.map.with_index { |bit, index| bit ^ other[index] }
|
27
40
|
end
|
28
41
|
|
29
42
|
def +(other)
|
30
|
-
self.class.new
|
43
|
+
self.class.new vector + other.to_a
|
44
|
+
end
|
45
|
+
|
46
|
+
def addition_by_zeros(size: 512)
|
47
|
+
return self if vector.size >= size
|
48
|
+
self.class.new(Array.new(size - vector.size, 0) + @vector)
|
31
49
|
end
|
32
50
|
|
33
|
-
def
|
34
|
-
return self if
|
35
|
-
self.class.new(
|
51
|
+
def addition_bit_padding(size: 512)
|
52
|
+
return self if vector.size >= size
|
53
|
+
(self.class.new([1]) + vector).addition_by_zeros(size: 512)
|
36
54
|
end
|
37
55
|
|
38
56
|
def to_dec
|
@@ -44,30 +62,30 @@ module Stribog
|
|
44
62
|
end
|
45
63
|
|
46
64
|
def to_s
|
47
|
-
|
65
|
+
vector.join
|
48
66
|
end
|
49
67
|
|
50
68
|
def size
|
51
|
-
|
69
|
+
vector.size
|
52
70
|
end
|
53
71
|
|
54
72
|
def [](index)
|
55
|
-
|
73
|
+
vector[index]
|
56
74
|
end
|
57
75
|
|
58
76
|
def each
|
59
|
-
|
77
|
+
vector.each do |v|
|
60
78
|
yield(v)
|
61
79
|
end
|
62
80
|
end
|
63
81
|
|
64
82
|
def to_a
|
65
|
-
|
83
|
+
vector
|
66
84
|
end
|
67
85
|
|
68
86
|
def to_byte_array
|
69
|
-
raise 'DimensionError' unless (
|
70
|
-
|
87
|
+
raise 'DimensionError' unless (vector.size % 8).zero?
|
88
|
+
vector.each_slice(8).map { |byte| byte.join.to_i(2) }
|
71
89
|
end
|
72
90
|
|
73
91
|
def zero?
|
@@ -77,11 +95,11 @@ module Stribog
|
|
77
95
|
private
|
78
96
|
|
79
97
|
def binary?
|
80
|
-
|
98
|
+
vector.all? { |el| [0, 1].include? el }
|
81
99
|
end
|
82
100
|
|
83
|
-
def according_dimension?(
|
84
|
-
|
101
|
+
def according_dimension?(second_vector)
|
102
|
+
vector.size == second_vector.size
|
85
103
|
end
|
86
104
|
end
|
87
105
|
end
|
data/lib/stribog/compression.rb
CHANGED
@@ -13,7 +13,7 @@ module Stribog
|
|
13
13
|
@hash_vector = hash_vector
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def call
|
17
17
|
vector = lpsx_func @n, @hash_vector
|
18
18
|
vector = func_e vector, @message
|
19
19
|
vector = vector ^ @hash_vector
|
@@ -62,7 +62,7 @@ module Stribog
|
|
62
62
|
BinaryVector.from_byte(
|
63
63
|
not_zeros_indexes(vector)
|
64
64
|
.inject(0) { |acc, elem| acc ^ MATRIX_A[elem] }
|
65
|
-
).
|
65
|
+
).addition_by_zeros(size: 64)
|
66
66
|
end
|
67
67
|
|
68
68
|
# rubocop:disable Style/EachWithObject
|
data/lib/stribog/create_hash.rb
CHANGED
@@ -4,97 +4,127 @@ module Stribog
|
|
4
4
|
#
|
5
5
|
# @author WildDima
|
6
6
|
class CreateHash
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :binary_vector, :message_adapter, :message, :digest_length, :message_vector
|
8
8
|
|
9
9
|
HASH_LENGTH = 512
|
10
10
|
|
11
|
-
def initialize(message,
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@sum = new_binary_vector(Array.new(HASH_LENGTH, 0))
|
11
|
+
def initialize(message, binary_vector: BinaryVector)
|
12
|
+
@binary_vector = binary_vector
|
13
|
+
@message = binary_vector.from_hex(message)
|
14
|
+
@n = binary_vector_field_by
|
15
|
+
@sum = binary_vector_field_by
|
17
16
|
end
|
18
17
|
|
19
|
-
def call(digest_length
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
18
|
+
def call(digest_length = HASH_LENGTH)
|
19
|
+
create_hash_params!(digest_length: digest_length)
|
20
|
+
|
21
|
+
return_hash(
|
22
|
+
final_compression(
|
23
|
+
core_hashing(
|
24
|
+
compact_message(
|
25
|
+
sum: @sum,
|
26
|
+
n: @n,
|
27
|
+
message_vector: message_vector,
|
28
|
+
hash_vector: @hash_vector
|
29
|
+
)
|
30
|
+
)
|
31
|
+
)
|
32
|
+
)
|
33
|
+
end
|
32
34
|
|
33
|
-
|
35
|
+
private
|
34
36
|
|
35
|
-
|
37
|
+
def create_hash_params!(digest_length:)
|
38
|
+
@digest_length = digest_length
|
39
|
+
@hash_vector = create_hash_vector
|
40
|
+
@message_vector = message.dup
|
36
41
|
end
|
37
42
|
|
38
|
-
|
39
|
-
def hash_vector
|
43
|
+
def create_hash_vector
|
40
44
|
case digest_length
|
41
45
|
when 512
|
42
|
-
|
46
|
+
binary_vector_field_by(size: 512)
|
43
47
|
when 256
|
44
|
-
|
48
|
+
binary_vector_from_array(Array.new(64, '00000001').join.chars.map(&:to_i))
|
45
49
|
else
|
46
50
|
raise ArgumentError,
|
47
51
|
"digest length must be equal to 256 or 512, not #{digest_length}"
|
48
52
|
end
|
49
53
|
end
|
50
54
|
|
51
|
-
|
55
|
+
def compact_message(sum:, n:, message_vector:, hash_vector:, message_head: nil)
|
56
|
+
if message_vector.size < HASH_LENGTH || !message_head.nil? && message_head.size < HASH_LENGTH
|
57
|
+
return { sum: sum, n: n, message_vector: message_head || message_vector,
|
58
|
+
hash_vector: hash_vector }
|
59
|
+
end
|
52
60
|
|
53
|
-
|
61
|
+
compact_message(
|
62
|
+
sum: addition_in_ring_to_binary(sum.to_dec, message_vector.to_dec),
|
63
|
+
n: addition_in_ring_to_binary(n.to_dec, slice_message_tail(message_vector).size),
|
64
|
+
message_vector: message_vector,
|
65
|
+
hash_vector: compress(n: n, message: slice_message_tail(message_vector),
|
66
|
+
hash_vector: hash_vector),
|
67
|
+
message_head: slice_message_head(message_vector)
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def slice_message_head(message_vector)
|
72
|
+
binary_vector_from_array(message_vector.vector[0...-HASH_LENGTH])
|
73
|
+
end
|
74
|
+
|
75
|
+
def slice_message_tail(message_vector)
|
76
|
+
binary_vector_from_array(message_vector.vector[-HASH_LENGTH..-1])
|
77
|
+
end
|
78
|
+
|
79
|
+
def core_hashing(sum:, n:, message_vector:, hash_vector:)
|
80
|
+
new_sum = addition_in_ring_to_binary(sum.to_dec,
|
81
|
+
message_vector
|
82
|
+
.addition_bit_padding(size: HASH_LENGTH)
|
83
|
+
.to_dec)
|
84
|
+
|
85
|
+
new_n = addition_in_ring_to_binary(n.to_dec, message_vector.size)
|
86
|
+
|
87
|
+
new_hash_vector = compress(n: n.addition_by_zeros(size: HASH_LENGTH),
|
88
|
+
message: message_vector.addition_bit_padding(size: HASH_LENGTH),
|
89
|
+
hash_vector: hash_vector)
|
90
|
+
|
91
|
+
{ sum: new_sum, n: new_n, hash_vector: new_hash_vector }
|
92
|
+
end
|
93
|
+
|
94
|
+
def final_compression(sum:, n:, hash_vector:)
|
95
|
+
compress(message: sum, hash_vector: compress(message: n, hash_vector: hash_vector))
|
96
|
+
end
|
97
|
+
|
98
|
+
def return_hash(final_vector)
|
54
99
|
case digest_length
|
55
100
|
when 512
|
56
|
-
|
101
|
+
final_vector
|
57
102
|
when 256
|
58
|
-
|
103
|
+
binary_vector_from_array(final_vector[0..255])
|
59
104
|
else
|
60
105
|
raise ArgumentError,
|
61
106
|
"digest length must be equal to 256 or 512, not #{digest_length}"
|
62
107
|
end
|
63
108
|
end
|
64
109
|
|
65
|
-
def message_cut!(sum:, n:, message:, hash_vector:)
|
66
|
-
@hash_vector = compress(n: n, message: message.vector, hash_vector: hash_vector)
|
67
|
-
@n = addition_in_ring_to_binary(n.to_dec, message.vector.size)
|
68
|
-
@sum = addition_in_ring_to_binary(sum.to_dec, message.vector.to_dec)
|
69
|
-
end
|
70
|
-
|
71
|
-
def core_hashing!(sum:, n:, message:, hash_vector:)
|
72
|
-
@hash_vector = compress(n: n.addition_to(size: HASH_LENGTH),
|
73
|
-
message: message.addition_to(size: HASH_LENGTH),
|
74
|
-
hash_vector: hash_vector)
|
75
|
-
@n = addition_in_ring_to_binary(n.to_dec, message.size)
|
76
|
-
@sum = addition_in_ring_to_binary(sum.to_dec, message.addition_to(size: HASH_LENGTH).to_dec)
|
77
|
-
end
|
78
|
-
|
79
110
|
def addition_in_ring(first, second, ring)
|
80
111
|
(first + second) % ring
|
81
112
|
end
|
82
113
|
|
83
114
|
def addition_in_ring_to_binary(first, second, ring = 2**HASH_LENGTH, size: HASH_LENGTH)
|
84
|
-
|
115
|
+
binary_vector.from_byte(addition_in_ring(first, second, ring), size: size)
|
85
116
|
end
|
86
117
|
|
87
|
-
def compress(message:, hash_vector:, n:
|
88
|
-
|
89
|
-
Compression.new(n, message, hash_vector).start
|
118
|
+
def compress(message:, hash_vector:, n: binary_vector_field_by)
|
119
|
+
Compression.new(n, message, hash_vector).call
|
90
120
|
end
|
91
121
|
|
92
|
-
def
|
93
|
-
|
122
|
+
def binary_vector_from_array(vector)
|
123
|
+
binary_vector.new(vector)
|
94
124
|
end
|
95
125
|
|
96
|
-
def
|
97
|
-
|
126
|
+
def binary_vector_field_by(size: HASH_LENGTH, value: 0)
|
127
|
+
binary_vector_from_array(Array.new(size, value))
|
98
128
|
end
|
99
129
|
end
|
100
130
|
end
|
data/lib/stribog/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stribog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WildDima
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,7 +103,6 @@ files:
|
|
103
103
|
- lib/stribog/compression.rb
|
104
104
|
- lib/stribog/create_hash.rb
|
105
105
|
- lib/stribog/hash_params.rb
|
106
|
-
- lib/stribog/message.rb
|
107
106
|
- lib/stribog/version.rb
|
108
107
|
- stribog.gemspec
|
109
108
|
homepage: https://github.com/WildDima/stribog
|
data/lib/stribog/message.rb
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
module Stribog
|
2
|
-
# Message
|
3
|
-
#
|
4
|
-
# @author WildDima
|
5
|
-
class Message
|
6
|
-
private_class_method :new
|
7
|
-
|
8
|
-
attr_accessor :message, :vector
|
9
|
-
|
10
|
-
def initialize(message, vector)
|
11
|
-
@message = message
|
12
|
-
@vector = vector
|
13
|
-
end
|
14
|
-
|
15
|
-
class << self
|
16
|
-
def from_bin(bin)
|
17
|
-
new bin_to_hex(bin), bin
|
18
|
-
end
|
19
|
-
|
20
|
-
def from_hex(hex)
|
21
|
-
new hex, hex_to_bin(hex)
|
22
|
-
end
|
23
|
-
|
24
|
-
def from_string(string)
|
25
|
-
new string, BinaryVector.new(string.unpack('B*')[0].chars.map(&:to_i))
|
26
|
-
end
|
27
|
-
|
28
|
-
def from_path(path)
|
29
|
-
file = File.read(path)
|
30
|
-
new file, file.unpack('B*')
|
31
|
-
end
|
32
|
-
|
33
|
-
def hex_to_bin(hex)
|
34
|
-
BinaryVector.new(hex.chars.map do |x|
|
35
|
-
bin = x.to_i(16).to_s(2)
|
36
|
-
'0' * (4 - bin.length) + bin
|
37
|
-
end.join.chars.map(&:to_i))
|
38
|
-
end
|
39
|
-
|
40
|
-
def bin_to_hex(bin)
|
41
|
-
bin.to_dec.to_s(16)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def addition_to(size: 512)
|
46
|
-
return if vector.size >= size
|
47
|
-
(BinaryVector.new([1]) + vector).addition_to(size: 512)
|
48
|
-
end
|
49
|
-
|
50
|
-
def size
|
51
|
-
@vector.size
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def to_vector(message)
|
57
|
-
BinaryVector.new message.to_bits
|
58
|
-
end
|
59
|
-
|
60
|
-
def to_bits(byteorder: :big)
|
61
|
-
case byteorder
|
62
|
-
when :big
|
63
|
-
message.unpack('B*')
|
64
|
-
when :small
|
65
|
-
message.unpack('b*')
|
66
|
-
else
|
67
|
-
raise ArgumentError,
|
68
|
-
"byteorder must be equal to :big or :small, not: #{byteorder}"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|