sha512t 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sha512.rb +10 -37
  3. metadata +1 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2829c30ba19cb48066239992d14753ec4d546c49
4
- data.tar.gz: a68b9c8fc14233070fa58a5a35e592b1c5e32ab7
3
+ metadata.gz: b00ec75a952b8e835cd734ba306841e2e8816b3f
4
+ data.tar.gz: cebcb2b1aa7246a1a52e7f3eadf2bd338061bd91
5
5
  SHA512:
6
- metadata.gz: 03f933eb7dcc7ffedf728a7c9dfe68bffde142666a39096d03d8b3a3ed807cba61cf5ac8f5d37652350f5ea7214c4e19619a79269a089aa9cc5a730de72ddb26
7
- data.tar.gz: 87606be5de26152b4e6df998e4d431a630b4812d16f80e0b4420cdc4e59bbba4ac19ddc51c6a292cbc363e31d63b429d5e7bf97ae3c65d23b4e0251f73946526
6
+ metadata.gz: df627c2249fb3685ab277ffa5f9bf8b76fc09454a42a3ae6fd6c984086730ce9a9cfd5023ff06a5a17fffc3256aea62027ed375be50ec3df344a9e1b70ad607e
7
+ data.tar.gz: 4ec1836c8022c5340d02be78a7c372e8d27745a07dc4a69642aaaad15dcb7e2e9572b64ea08d895ebcc894999fed49b3e72fe3ff049d82d14d529737e2d0b001
data/lib/sha512.rb CHANGED
@@ -41,44 +41,21 @@ class Sha512
41
41
  end
42
42
 
43
43
  def preprocessing(message)
44
- # Preprocessing Step 1: Padding the message
45
- bytes = message.bytes.to_a
46
- l = bytes.size * 8
47
- padded_message = ""
48
- zero = "0"
49
-
50
- bytes.each do |byte|
51
- bit = byte.to_s(2)
52
- if bit.length < 8
53
- bit = (zero * (8 - bit.length)) + bit
54
- end
55
- padded_message += bit
56
- end
44
+ # Preprocessing Step 1: Padding the message
45
+ preprocessed_message = message.unpack('B*')[0]
46
+ message_length = preprocessed_message.length
57
47
 
58
48
  # padding with a one bit
59
- padded_message += "1"
49
+ preprocessed_message += "1"
60
50
 
61
51
  # add k zero bits, where k is the smallest non-negative solution to the equation l+1+k = 896 mod 1024
62
- blocks = (896-(l+1))
63
- while blocks < 0 do
64
- blocks += 1024
65
- end
66
- padded_message += zero * blocks
52
+ preprocessed_message += "0" * ((896-(message_length+1)) % 1024)
67
53
 
68
54
  # append 128-bit block that is equal to the number of l expressed using a binary representation
69
- last_bit_block = l.to_s(2)
70
- if last_bit_block.length < 128
71
- last_bit_block = (zero * (128 - last_bit_block.length)) + last_bit_block
72
- end
73
- padded_message += last_bit_block
55
+ preprocessed_message += message_length.to_s(2).rjust(128, '0')
74
56
 
75
57
  # Preprocessing Step 2: Parsing the message
76
- parsed_message = []
77
- for block in 0..padded_message.size/64-1
78
- parsed_message.push(padded_message[(64*block)..(64*block)+63])
79
- end
80
-
81
- return parsed_message
58
+ return preprocessed_message.chars.each_slice(64).map(&:join)
82
59
  end
83
60
 
84
61
  def hash_computation(p, iv)
@@ -139,11 +116,7 @@ class Sha512
139
116
  # Convert hash to hex-String
140
117
  hash_string = ""
141
118
  hash.each do |h|
142
- temp = h.to_s(16)
143
- if temp.length < 16
144
- temp = "0" * (16 - temp.length) + temp
145
- end
146
- hash_string += temp
119
+ hash_string += h.to_s(16).rjust(16, '0')
147
120
  end
148
121
  return hash_string
149
122
  end
@@ -165,9 +138,9 @@ class Sha512
165
138
  # consists of two steps:
166
139
  # 1. padding the message,
167
140
  # 2. parsing the message into message blocks
168
- parsed_message = Sha512.new.preprocessing(message)
141
+ preprocessed_massage = Sha512.new.preprocessing(message)
169
142
 
170
143
  # Hash Computation
171
- return Sha512.new.hash_computation(parsed_message, sha512_initial_hash_value)
144
+ return Sha512.new.hash_computation(preprocessed_massage, sha512_initial_hash_value)
172
145
  end
173
146
  end
metadata CHANGED
@@ -1,11 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sha512t
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Walch
8
- - Maximilian Bielefeld
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []