stribog 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/stribog.rb +10 -0
- data/lib/stribog/binary_vector.rb +87 -0
- data/lib/stribog/compression.rb +89 -0
- data/lib/stribog/create_hash.rb +100 -0
- data/lib/stribog/hash_params.rb +53 -0
- data/lib/stribog/message.rb +72 -0
- data/lib/stribog/version.rb +3 -0
- data/stribog.gemspec +38 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6dbadae89288325c543d7ddfbd077e59daa953ca
|
4
|
+
data.tar.gz: 8b2957b6d6eed667eeed5ca9a8908c665a0f4a35
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 672923eb97afdddf14f12123799400f57445fdda8302ce3d6a4b9b8d70153e20821114db301558e225d17d4738f5780822062afdbb202bd9a54e6402a4403a0a
|
7
|
+
data.tar.gz: 519f6bf11aac2e307ac7dc8bd6f87c4ea1ef5fd7877c38c21511aee88074b5cc001d5eccc0dba4684964f3fd241d0e8b660fa4492042b2fbb60a0c5026a1d403
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- crypto_gost.gemspec
|
4
|
+
- Rakefile
|
5
|
+
- bin/test.rb
|
6
|
+
- lib/crypto_gost/digital_signature_gost_12/modular_arithmetic.rb
|
7
|
+
- lib/crypto_gost/digital_signature_gost_12/group/**/*
|
8
|
+
- spec/**/*
|
9
|
+
- stribog.gemspec
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 100
|
13
|
+
|
14
|
+
Metrics/MethodLength:
|
15
|
+
Max: 13
|
16
|
+
|
17
|
+
Metrics/AbcSize:
|
18
|
+
Max: 20
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at dtopornin@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 WildDima
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Stribog
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stribog`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'stribog'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install stribog
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stribog. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'stribog'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/stribog.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'stribog/version'
|
2
|
+
# Stribog gost
|
3
|
+
#
|
4
|
+
# @author WildDima
|
5
|
+
module Stribog
|
6
|
+
require_relative './stribog/create_hash'
|
7
|
+
require_relative './stribog/compression'
|
8
|
+
require_relative './stribog/binary_vector'
|
9
|
+
require_relative './stribog/message'
|
10
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Stribog
|
2
|
+
# BinaryVector
|
3
|
+
#
|
4
|
+
# @author WildDima
|
5
|
+
class BinaryVector
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def from_byte(byte, size: 8)
|
10
|
+
new(byte.to_s(2).chars.map(&:to_i)).addition_to(size: size)
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_byte_array(byte_array, size: 8)
|
14
|
+
new(byte_array.map { |byte| from_byte(byte, size: size).to_a }
|
15
|
+
.inject([]) { |acc, elem| acc + elem })
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(vector)
|
20
|
+
@vector = vector
|
21
|
+
raise 'NotBinaryError' unless binary?
|
22
|
+
end
|
23
|
+
|
24
|
+
def ^(other)
|
25
|
+
raise 'DimensionError' unless according_dimension?(other)
|
26
|
+
self.class.new @vector.map.with_index { |bit, index| bit ^ other[index] }
|
27
|
+
end
|
28
|
+
|
29
|
+
def +(other)
|
30
|
+
self.class.new @vector + other.to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
def addition_to(size: 512)
|
34
|
+
return self if @vector.size >= size
|
35
|
+
self.class.new(Array.new(size - @vector.size, 0) + @vector)
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_dec
|
39
|
+
to_s.to_i(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_hex
|
43
|
+
to_s.to_i(2).to_s(16)
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
@vector.join
|
48
|
+
end
|
49
|
+
|
50
|
+
def size
|
51
|
+
@vector.size
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](index)
|
55
|
+
@vector[index]
|
56
|
+
end
|
57
|
+
|
58
|
+
def each
|
59
|
+
@vector.each do |v|
|
60
|
+
yield(v)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_a
|
65
|
+
@vector
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_byte_array
|
69
|
+
raise 'DimensionError' unless (@vector.size % 8).zero?
|
70
|
+
@vector.each_slice(8).map { |byte| byte.join.to_i(2) }
|
71
|
+
end
|
72
|
+
|
73
|
+
def zero?
|
74
|
+
to_dec.zero?
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def binary?
|
80
|
+
@vector.all? { |el| [0, 1].include? el }
|
81
|
+
end
|
82
|
+
|
83
|
+
def according_dimension?(vector)
|
84
|
+
@vector.size == vector.size
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require_relative 'hash_params'
|
2
|
+
|
3
|
+
module Stribog
|
4
|
+
# Compression
|
5
|
+
#
|
6
|
+
# @author WildDima
|
7
|
+
class Compression
|
8
|
+
include HashParams
|
9
|
+
|
10
|
+
def initialize(n, message, hash_vector)
|
11
|
+
@n = n
|
12
|
+
@message = message
|
13
|
+
@hash_vector = hash_vector
|
14
|
+
end
|
15
|
+
|
16
|
+
def start
|
17
|
+
vector = lpsx_func @n, @hash_vector
|
18
|
+
vector = func_e vector, @message
|
19
|
+
vector = vector ^ @hash_vector
|
20
|
+
vector ^ @message
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def lpsx_func(first_vector, second_vector)
|
26
|
+
linear_transformation(
|
27
|
+
permutation_t(
|
28
|
+
replacement_pi(
|
29
|
+
first_vector ^ second_vector
|
30
|
+
)
|
31
|
+
)
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def replacement_pi(vector)
|
36
|
+
BinaryVector.from_byte_array vector.to_byte_array
|
37
|
+
.map { |byte| PI[byte] }
|
38
|
+
end
|
39
|
+
|
40
|
+
# rubocop:disable Style/EachWithObject
|
41
|
+
def permutation_t(vector)
|
42
|
+
BinaryVector.from_byte_array(
|
43
|
+
vector.to_byte_array
|
44
|
+
.each.with_index.inject([]) do |b_arr, (byte, index)|
|
45
|
+
b_arr[T[index]] = byte
|
46
|
+
b_arr
|
47
|
+
end
|
48
|
+
)
|
49
|
+
end
|
50
|
+
# rubocop:enable Style/EachWithObject
|
51
|
+
|
52
|
+
def linear_transformation(vector)
|
53
|
+
BinaryVector.from_byte_array(
|
54
|
+
vector.each_slice(64).map do |byte8|
|
55
|
+
small_linear_transformation(BinaryVector.new(byte8)).to_dec
|
56
|
+
end,
|
57
|
+
size: 64
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
def small_linear_transformation(vector)
|
62
|
+
BinaryVector.from_byte(
|
63
|
+
not_zeros_indexes(vector)
|
64
|
+
.inject(0) { |acc, elem| acc ^ MATRIX_A[elem] }
|
65
|
+
).addition_to(size: 64)
|
66
|
+
end
|
67
|
+
|
68
|
+
# rubocop:disable Style/EachWithObject
|
69
|
+
def func_e(first_vector, second_vector)
|
70
|
+
vectors = CONSTANTS_C
|
71
|
+
.inject(v1: first_vector.dup,
|
72
|
+
v2: second_vector.dup) do |vs, const|
|
73
|
+
vs[:v2] = lpsx_func(vs[:v1], vs[:v2])
|
74
|
+
vs[:v1] = lpsx_func(vs[:v1], BinaryVector.from_byte(const.to_i(16),
|
75
|
+
size: 512))
|
76
|
+
vs
|
77
|
+
end
|
78
|
+
vectors[:v1] ^ vectors[:v2]
|
79
|
+
end
|
80
|
+
# rubocop:enable Style/EachWithObject
|
81
|
+
|
82
|
+
def not_zeros_indexes(vector)
|
83
|
+
vector.map.with_index do |bit, index|
|
84
|
+
next if bit.zero?
|
85
|
+
index
|
86
|
+
end.compact
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
module Stribog
|
3
|
+
# Hash function
|
4
|
+
#
|
5
|
+
# @author WildDima
|
6
|
+
class CreateHash
|
7
|
+
attr_reader :vector_adapter, :message_adapter, :message, :digest_length
|
8
|
+
|
9
|
+
HASH_LENGTH = 512
|
10
|
+
|
11
|
+
def initialize(message, vector_adapter: BinaryVector, message_adapter: Message)
|
12
|
+
@vector_adapter = vector_adapter
|
13
|
+
@message_adapter = message_adapter
|
14
|
+
@message = message
|
15
|
+
@n = new_binary_vector(Array.new(HASH_LENGTH, 0))
|
16
|
+
@sum = new_binary_vector(Array.new(HASH_LENGTH, 0))
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(digest_length: HASH_LENGTH)
|
20
|
+
@digest_length = digest_length
|
21
|
+
@hash_vector = create_hash_vector
|
22
|
+
|
23
|
+
while message.size > HASH_LENGTH
|
24
|
+
message = new_message_from_bin new_binary_vector(@message.vector[-HASH_LENGTH..-1])
|
25
|
+
message_cut!(sum: @sum, n: @n, message: message, hash_vector: @hash_vector)
|
26
|
+
@message = new_message_from_bin new_binary_vector(@message.vector[0...-HASH_LENGTH])
|
27
|
+
end
|
28
|
+
|
29
|
+
core_hashing!(sum: @sum, n: @n, message: @message, hash_vector: @hash_vector)
|
30
|
+
|
31
|
+
@hash_vector = compress(message: @n, hash_vector: @hash_vector)
|
32
|
+
|
33
|
+
@hash_vector = compress(message: @sum, hash_vector: @hash_vector)
|
34
|
+
|
35
|
+
hash_vector
|
36
|
+
end
|
37
|
+
|
38
|
+
# TODO: MORE DRY
|
39
|
+
def hash_vector
|
40
|
+
case digest_length
|
41
|
+
when 512
|
42
|
+
@hash_vector
|
43
|
+
when 256
|
44
|
+
new_binary_vector(@hash_vector[0..255])
|
45
|
+
else
|
46
|
+
raise ArgumentError,
|
47
|
+
"digest length must be equal to 256 or 512, not #{digest_length}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def create_hash_vector
|
54
|
+
case digest_length
|
55
|
+
when 512
|
56
|
+
new_binary_vector(Array.new(512, 0))
|
57
|
+
when 256
|
58
|
+
new_binary_vector(Array.new(64, '00000001').join.chars.map(&:to_i))
|
59
|
+
else
|
60
|
+
raise ArgumentError,
|
61
|
+
"digest length must be equal to 256 or 512, not #{digest_length}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
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
|
+
def addition_in_ring(first, second, ring)
|
80
|
+
(first + second) % ring
|
81
|
+
end
|
82
|
+
|
83
|
+
def addition_in_ring_to_binary(first, second, ring = 2**HASH_LENGTH, size: HASH_LENGTH)
|
84
|
+
vector_adapter.from_byte(addition_in_ring(first, second, ring), size: size)
|
85
|
+
end
|
86
|
+
|
87
|
+
def compress(message:, hash_vector:, n: nil)
|
88
|
+
n ||= new_binary_vector(Array.new(HASH_LENGTH, 0))
|
89
|
+
Compression.new(n, message, hash_vector).start
|
90
|
+
end
|
91
|
+
|
92
|
+
def new_message_from_bin(bin)
|
93
|
+
message_adapter.from_bin(bin)
|
94
|
+
end
|
95
|
+
|
96
|
+
def new_binary_vector(vector)
|
97
|
+
vector_adapter.new(vector)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Stribog
|
2
|
+
# rubocop:disable Metrics/LineLength
|
3
|
+
module HashParams
|
4
|
+
PI = [252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77, 233, 119, 240,
|
5
|
+
219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193, 249, 24, 101, 90, 226, 92, 239,
|
6
|
+
33, 129, 28, 60, 66, 139, 1, 142, 79, 5, 132, 2, 174, 227, 106, 143, 160, 6, 11, 237, 152, 127,
|
7
|
+
212, 211, 31, 235, 52, 44, 81, 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206, 204, 181,
|
8
|
+
112, 14, 86, 8, 12, 118, 18, 191, 114, 19, 71, 156, 183, 93, 135, 21, 161, 150, 41, 16, 123,
|
9
|
+
154, 199, 243, 145, 120, 111, 157, 158, 178, 177, 50, 117, 25, 61, 255, 53, 138, 126, 109,
|
10
|
+
84, 198, 128, 195, 189, 13, 87, 223, 245, 36, 169, 62, 168, 67, 201, 215, 121, 214, 246, 124,
|
11
|
+
34, 185, 3, 224, 15, 236, 222, 122, 148, 176, 188, 220, 232, 40, 80, 78, 51, 10, 74, 167, 151,
|
12
|
+
96, 115, 30, 0, 98, 68, 26, 184, 56, 130, 100, 159, 38, 65, 173, 69, 70, 146, 39, 94, 85, 47,
|
13
|
+
140, 163, 165, 125, 105, 213, 149, 59, 7, 88, 179, 64, 134, 172, 29, 247, 48, 55, 107, 228,
|
14
|
+
136, 217, 231, 137, 225, 27, 131, 73, 76, 63, 248, 254, 141, 83, 170, 144, 202, 216, 133, 97,
|
15
|
+
32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208, 190, 229, 108, 82, 89, 166, 116, 210,
|
16
|
+
230, 244, 180, 192, 209, 102, 175, 194, 57, 75, 99, 182].freeze
|
17
|
+
|
18
|
+
T = [0, 8, 16, 24, 32, 40, 48, 56, 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34, 42, 50, 58,
|
19
|
+
3, 11, 19, 27, 35, 43, 51, 59, 4, 12, 20, 28, 36, 44, 52, 60, 5, 13, 21, 29, 37, 45, 53, 61, 6, 14,
|
20
|
+
22, 30, 38, 46, 54, 62, 7, 15, 23, 31, 39, 47, 55, 63].freeze
|
21
|
+
|
22
|
+
MATRIX_A = [0x8e20faa72ba0b470, 0x47107ddd9b505a38, 0xad08b0e0c3282d1c, 0xd8045870ef14980e,
|
23
|
+
0x6c022c38f90a4c07, 0x3601161cf205268d, 0x1b8e0b0e798c13c8, 0x83478b07b2468764,
|
24
|
+
0xa011d380818e8f40, 0x5086e740ce47c920, 0x2843fd2067adea10, 0x14aff010bdd87508,
|
25
|
+
0x0ad97808d06cb404, 0x05e23c0468365a02, 0x8c711e02341b2d01, 0x46b60f011a83988e,
|
26
|
+
0x90dab52a387ae76f, 0x486dd4151c3dfdb9, 0x24b86a840e90f0d2, 0x125c354207487869,
|
27
|
+
0x092e94218d243cba, 0x8a174a9ec8121e5d, 0x4585254f64090fa0, 0xaccc9ca9328a8950,
|
28
|
+
0x9d4df05d5f661451, 0xc0a878a0a1330aa6, 0x60543c50de970553, 0x302a1e286fc58ca7,
|
29
|
+
0x18150f14b9ec46dd, 0x0c84890ad27623e0, 0x0642ca05693b9f70, 0x0321658cba93c138,
|
30
|
+
0x86275df09ce8aaa8, 0x439da0784e745554, 0xafc0503c273aa42a, 0xd960281e9d1d5215,
|
31
|
+
0xe230140fc0802984, 0x71180a8960409a42, 0xb60c05ca30204d21, 0x5b068c651810a89e,
|
32
|
+
0x456c34887a3805b9, 0xac361a443d1c8cd2, 0x561b0d22900e4669, 0x2b838811480723ba,
|
33
|
+
0x9bcf4486248d9f5d, 0xc3e9224312c8c1a0, 0xeffa11af0964ee50, 0xf97d86d98a327728,
|
34
|
+
0xe4fa2054a80b329c, 0x727d102a548b194e, 0x39b008152acb8227, 0x9258048415eb419d,
|
35
|
+
0x492c024284fbaec0, 0xaa16012142f35760, 0x550b8e9e21f7a530, 0xa48b474f9ef5dc18,
|
36
|
+
0x70a6a56e2440598e, 0x3853dc371220a247, 0x1ca76e95091051ad, 0x0edd37c48a08a6d8,
|
37
|
+
0x07e095624504536c, 0x8d70c431ac02a736, 0xc83862965601dd1b, 0x641c314b2b8ee083].freeze
|
38
|
+
|
39
|
+
CONSTANTS_C = %w(b1085bda1ecadae9ebcb2f81c0657c1f2f6a76432e45d016714eb88d7585c4fc4b7ce09192676901a2422a08a460d31505767436cc744d23dd806559f2a64507
|
40
|
+
6fa3b58aa99d2f1a4fe39d460f70b5d7f3feea720a232b9861d55e0f16b501319ab5176b12d699585cb561c2db0aa7ca55dda21bd7cbcd56e679047021b19bb7
|
41
|
+
f574dcac2bce2fc70a39fc286a3d843506f15e5f529c1f8bf2ea7514b1297b7bd3e20fe490359eb1c1c93a376062db09c2b6f443867adb31991e96f50aba0ab2
|
42
|
+
ef1fdfb3e81566d2f948e1a05d71e4dd488e857e335c3c7d9d721cad685e353fa9d72c82ed03d675d8b71333935203be3453eaa193e837f1220cbebc84e3d12e
|
43
|
+
4bea6bacad4747999a3f410c6ca923637f151c1f1686104a359e35d7800fffbdbfcd1747253af5a3dfff00b723271a167a56a27ea9ea63f5601758fd7c6cfe57
|
44
|
+
ae4faeae1d3ad3d96fa4c33b7a3039c02d66c4f95142a46c187f9ab49af08ec6cffaa6b71c9ab7b40af21f66c2bec6b6bf71c57236904f35fa68407a46647d6e
|
45
|
+
f4c70e16eeaac5ec51ac86febf240954399ec6c7e6bf87c9d3473e33197a93c90992abc52d822c3706476983284a05043517454ca23c4af38886564d3a14d493
|
46
|
+
9b1f5b424d93c9a703e7aa020c6e41414eb7f8719c36de1e89b4443b4ddbc49af4892bcb929b069069d18d2bd1a5c42f36acc2355951a8d9a47f0dd4bf02e71e
|
47
|
+
378f5a541631229b944c9ad8ec165fde3a7d3a1b258942243cd955b7e00d0984800a440bdbb2ceb17b2b8a9aa6079c540e38dc92cb1f2a607261445183235adb
|
48
|
+
abbedea680056f52382ae548b2e4f3f38941e71cff8a78db1fffe18a1b3361039fe76702af69334b7a1e6c303b7652f43698fad1153bb6c374b4c7fb98459ced
|
49
|
+
7bcd9ed0efc889fb3002c6cd635afe94d8fa6bbbebab076120018021148466798a1d71efea48b9caefbacd1d7d476e98dea2594ac06fd85d6bcaa4cd81f32d1b
|
50
|
+
378ee767f11631bad21380b00449b17acda43c32bcdf1d77f82012d430219f9b5d80ef9d1891cc86e71da4aa88e12852faf417d5d9b21b9948bc924af11bd720).freeze
|
51
|
+
# rubocop:enable Metrics/LineLength
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,72 @@
|
|
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
|
data/stribog.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stribog/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "stribog"
|
8
|
+
spec.version = Stribog::VERSION
|
9
|
+
spec.authors = ["WildDima"]
|
10
|
+
spec.email = ["dtopornin@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'Ruby library implementing GOST 34.11-12 Stribog'
|
13
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/WildDima/stribog"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "rubocop", "~> 0.45.0"
|
37
|
+
spec.add_development_dependency "byebug", "~> 9.0"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stribog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WildDima
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.45.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.45.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '9.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '9.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- dtopornin@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/stribog.rb
|
102
|
+
- lib/stribog/binary_vector.rb
|
103
|
+
- lib/stribog/compression.rb
|
104
|
+
- lib/stribog/create_hash.rb
|
105
|
+
- lib/stribog/hash_params.rb
|
106
|
+
- lib/stribog/message.rb
|
107
|
+
- lib/stribog/version.rb
|
108
|
+
- stribog.gemspec
|
109
|
+
homepage: https://github.com/WildDima/stribog
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.6.8
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Ruby library implementing GOST 34.11-12 Stribog
|
133
|
+
test_files: []
|