stribog 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ab38ecc83a74975f4d9c87d63269e294f9b2189
4
- data.tar.gz: 8ce92d234b2715149416ae0c961ddda76630112f
3
+ metadata.gz: fc696f1b18cb6ec04fce7cf3cc740be1eed204a6
4
+ data.tar.gz: ad56184c6dae90af01b9edd83424e7ba1864e2d4
5
5
  SHA512:
6
- metadata.gz: 1cb1ecedd099cad14175d362fd1d11864cec8e4d64fdc72b25cc57157387bf02f3b17abe77207b0510f6b8f73afb86a30563a4af3631c2ae6609641fb7836e8c
7
- data.tar.gz: 2df83ff564cd4360b888abe9a0b809c5965004a9ad974a67d51a95d847fa53c55a5414b226006f0b99755122a4d4677c26640c764a5d8ae87efa91de20a3b40c
6
+ metadata.gz: e02b4f9c61c008e4d0a4b413c0739d8866d645d12152829fd6fda289c524e2baba94e787ba40c04a1734a80e33d9ad70f8c1df2378e493a6e885792902fadafa
7
+ data.tar.gz: fde879defcd83fb2fb86910fe2c2b2c3177cde3c3580db70551573d6870073b5dcfeb863cd02a7b8854609adac733b4347f592a839cd9e0b9b20043e9109e8ac
@@ -1,12 +1,10 @@
1
1
  AllCops:
2
2
  Exclude:
3
- - crypto_gost.gemspec
3
+ - stribog.gemspec
4
4
  - Rakefile
5
5
  - bin/test.rb
6
- - lib/crypto_gost/digital_signature_gost_12/modular_arithmetic.rb
7
- - lib/crypto_gost/digital_signature_gost_12/group/**/*
6
+ - lib/stribog/hash_params.rb
8
7
  - spec/**/*
9
- - stribog.gemspec
10
8
 
11
9
  Metrics/LineLength:
12
10
  Max: 100
@@ -0,0 +1,44 @@
1
+ == Stribog
2
+
3
+ {<img src="https://travis-ci.org/WildDima/stribog.svg?branch=master"
4
+ alt="Build Status" />}[https://travis-ci.org/WildDima/stribog]
5
+
6
+ Ruby library implementing GOST 34.11-12 Stribog
7
+
8
+ === Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+
13
+ gem 'stribog'
14
+
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install stribog
23
+
24
+ === Usage
25
+
26
+ require 'stribog'
27
+
28
+ hash = Stribog::CreateHash.new('ruby')
29
+
30
+ # for 256 bit
31
+ hash.(256).to_hex #=> "cde7864b760a690f4ec24ffe5c66bd2c00db843e9cf1b82b6c467dba5a7846a7"
32
+
33
+ # for 512 bit
34
+ hash.(512).to_hex #=> "f0dafc0703f801d1ecbe7ff246808d07507f67879eaacf887ba041336fd51e0a964f00470ed0eedb0378a39e019eb308876ea8c84bb89e2c35159fb6dbbdc598"
35
+
36
+ === Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wilddima/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.
39
+
40
+
41
+ === License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
44
+
@@ -4,6 +4,8 @@ require 'stribog/version'
4
4
  # @author WildDima
5
5
  module Stribog
6
6
  require_relative './stribog/create_hash'
7
+ require_relative './stribog/hash_params'
7
8
  require_relative './stribog/compression'
8
9
  require_relative './stribog/binary_vector'
10
+ require_relative './stribog/digest'
9
11
  end
@@ -1,8 +1,7 @@
1
- require_relative 'hash_params'
2
-
3
1
  module Stribog
4
2
  # Compression
5
3
  #
4
+ # Class implements compression function of GOST R 34.11-2012 algorithm.
6
5
  # @author WildDima
7
6
  class Compression
8
7
  include HashParams
@@ -1,22 +1,47 @@
1
- require 'byebug'
2
1
  module Stribog
3
- # Hash function
2
+ # CreateHash
4
3
  #
4
+ # Class, which creates digests.
5
5
  # @author WildDima
6
6
  class CreateHash
7
- attr_reader :binary_vector, :message_adapter, :message, :digest_length, :message_vector
7
+ # Original message
8
+ #
9
+ # @api public
10
+ # @example
11
+ # hash.message
12
+ # @return [String] contains original message
13
+ attr_reader :message
14
+
15
+ # Length of digest. Should be equal to 256 or 512.
16
+ #
17
+ # @api public
18
+ # @example
19
+ # digest.digest_length
20
+ # @return [Fixnum] binary representation of digest
21
+ attr_reader :digest_length
22
+
23
+ # Contain message as instance of BinaryVector
24
+ #
25
+ # @api public
26
+ # @example
27
+ # digest.message_vector
28
+ # @return [BinaryVector] binary representation of message
29
+ attr_reader :message_vector
8
30
 
9
31
  HASH_LENGTH = 512
10
32
 
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
33
+ def initialize(message)
34
+ @message = message
16
35
  end
17
36
 
37
+ # Create digest of {#message}. Default equal to 512.
38
+ #
39
+ # @example
40
+ # Stribog::CreateHash.new('ruby').call(256)
41
+ # Stribog::CreateHash.new('ruby').call(512)
42
+ # @author WildDima
18
43
  def call(digest_length = HASH_LENGTH)
19
- create_hash_params!(digest_length: digest_length)
44
+ prepare_hash_params!(digest_length: digest_length)
20
45
 
21
46
  return_hash(
22
47
  final_compression(
@@ -34,12 +59,16 @@ module Stribog
34
59
 
35
60
  private
36
61
 
37
- def create_hash_params!(digest_length:)
62
+ # Create instance vars for hashing
63
+ def prepare_hash_params!(digest_length:)
64
+ @n = binary_vector_field_by
65
+ @sum = binary_vector_field_by
38
66
  @digest_length = digest_length
39
67
  @hash_vector = create_hash_vector
40
- @message_vector = message.dup
68
+ @message_vector = binary_vector.from_hex(message)
41
69
  end
42
70
 
71
+ # Create hash_vector for hashing
43
72
  def create_hash_vector
44
73
  case digest_length
45
74
  when 512
@@ -52,9 +81,10 @@ module Stribog
52
81
  end
53
82
  end
54
83
 
84
+ # Method, which slices longer messages, and hashings them by parts.
55
85
  def compact_message(sum:, n:, message_vector:, hash_vector:, message_head: nil)
56
86
  current_vector = message_head || message_vector
57
- if message_vector.size < HASH_LENGTH || !message_head.nil? && message_head.size < HASH_LENGTH
87
+ if current_vector.size < HASH_LENGTH
58
88
  return { sum: sum, n: n, message_vector: current_vector,
59
89
  hash_vector: hash_vector }
60
90
  end
@@ -69,14 +99,17 @@ module Stribog
69
99
  )
70
100
  end
71
101
 
102
+ # Method, which slices head of message
72
103
  def slice_message_head(message_vector)
73
104
  binary_vector_from_array(message_vector.vector[0...-HASH_LENGTH])
74
105
  end
75
106
 
107
+ # Method, which slices head of tail
76
108
  def slice_message_tail(message_vector)
77
109
  binary_vector_from_array(message_vector.vector[-HASH_LENGTH..-1])
78
110
  end
79
111
 
112
+ # Method, which implements main hashing
80
113
  def core_hashing(sum:, n:, message_vector:, hash_vector:)
81
114
  new_sum = addition_in_ring_to_binary(sum.to_dec,
82
115
  message_vector
@@ -92,40 +125,60 @@ module Stribog
92
125
  { sum: new_sum, n: new_n, hash_vector: new_hash_vector }
93
126
  end
94
127
 
128
+ # Method, which implements final compression
95
129
  def final_compression(sum:, n:, hash_vector:)
96
130
  compress(message: sum, hash_vector: compress(message: n, hash_vector: hash_vector))
97
131
  end
98
132
 
133
+ # Method, which return digest, dependent on them length
99
134
  def return_hash(final_vector)
100
135
  case digest_length
101
136
  when 512
102
- final_vector
137
+ create_digest(final_vector)
103
138
  when 256
104
- binary_vector_from_array(final_vector[0..255])
139
+ create_digest(binary_vector_from_array(final_vector[0..255]))
105
140
  else
106
141
  raise ArgumentError,
107
142
  "digest length must be equal to 256 or 512, not #{digest_length}"
108
143
  end
109
144
  end
110
145
 
146
+ # Method implements addition in ring
111
147
  def addition_in_ring(first, second, ring)
112
148
  (first + second) % ring
113
149
  end
114
150
 
151
+ # Method implements addition in ring and creates binary vector
115
152
  def addition_in_ring_to_binary(first, second, ring = 2**HASH_LENGTH, size: HASH_LENGTH)
116
153
  binary_vector.from_byte(addition_in_ring(first, second, ring), size: size)
117
154
  end
118
155
 
156
+ # Compression method
119
157
  def compress(message:, hash_vector:, n: binary_vector_field_by)
120
158
  Compression.new(n, message, hash_vector).call
121
159
  end
122
160
 
161
+ # Method creates binary vector from array
123
162
  def binary_vector_from_array(vector)
124
163
  binary_vector.new(vector)
125
164
  end
126
165
 
166
+ # Method creates binary vector and fields it by passed values
127
167
  def binary_vector_field_by(size: HASH_LENGTH, value: 0)
128
168
  binary_vector_from_array(Array.new(size, value))
129
169
  end
170
+
171
+ # Create new instance of Digest
172
+ def create_digest(binary_vector)
173
+ digest.new(binary_vector: binary_vector)
174
+ end
175
+
176
+ def binary_vector
177
+ @binary_vector ||= BinaryVector
178
+ end
179
+
180
+ def digest
181
+ @digest ||= Digest
182
+ end
130
183
  end
131
184
  end
@@ -0,0 +1,29 @@
1
+ module Stribog
2
+ # Digest
3
+ #
4
+ # Class, returning by CreateHash#call
5
+ # Contains binary and hex representation of digest.
6
+ # You can use {#binary} to get array and {#hex} to get digest in hash.
7
+ # @author WildDima
8
+ class Digest
9
+ # Contains binary representation of hash
10
+ #
11
+ # @api public
12
+ # @example
13
+ # digest.binary
14
+ # @return [Array] binary representation of digest
15
+ attr_reader :binary
16
+ # Contains hex value of hash
17
+ #
18
+ # @api public
19
+ # @example
20
+ # digest.hex
21
+ # @return [String] hex representation of digest
22
+ attr_reader :hex
23
+
24
+ def initialize(binary_vector:)
25
+ @binary = binary_vector.vector
26
+ @hex = binary_vector.to_hex
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,7 @@
1
1
  module Stribog
2
- # rubocop:disable Metrics/LineLength
2
+ # Contains constants for hashing
3
+ #
4
+ # @author WildDima
3
5
  module HashParams
4
6
  PI = [252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77, 233, 119, 240,
5
7
  219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193, 249, 24, 101, 90, 226, 92, 239,
@@ -48,6 +50,5 @@ module Stribog
48
50
  abbedea680056f52382ae548b2e4f3f38941e71cff8a78db1fffe18a1b3361039fe76702af69334b7a1e6c303b7652f43698fad1153bb6c374b4c7fb98459ced
49
51
  7bcd9ed0efc889fb3002c6cd635afe94d8fa6bbbebab076120018021148466798a1d71efea48b9caefbacd1d7d476e98dea2594ac06fd85d6bcaa4cd81f32d1b
50
52
  378ee767f11631bad21380b00449b17acda43c32bcdf1d77f82012d430219f9b5d80ef9d1891cc86e71da4aa88e12852faf417d5d9b21b9948bc924af11bd720).freeze
51
- # rubocop:enable Metrics/LineLength
52
53
  end
53
54
  end
@@ -1,3 +1,3 @@
1
1
  module Stribog
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
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.2
4
+ version: 0.1.3
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-02 00:00:00.000000000 Z
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,7 @@ files:
94
94
  - CODE_OF_CONDUCT.md
95
95
  - Gemfile
96
96
  - LICENSE.txt
97
- - README.md
97
+ - README.rdoc
98
98
  - Rakefile
99
99
  - bin/console
100
100
  - bin/setup
@@ -102,6 +102,7 @@ files:
102
102
  - lib/stribog/binary_vector.rb
103
103
  - lib/stribog/compression.rb
104
104
  - lib/stribog/create_hash.rb
105
+ - lib/stribog/digest.rb
105
106
  - lib/stribog/hash_params.rb
106
107
  - lib/stribog/version.rb
107
108
  - stribog.gemspec
data/README.md DELETED
@@ -1,41 +0,0 @@
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
-