sha256_length_extension_attack 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/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +14 -0
- data/exe/sha256_length_extension_attack +35 -0
- data/lib/sha256_length_extension_attack/sha256.rb +145 -0
- data/lib/sha256_length_extension_attack/version.rb +5 -0
- data/lib/sha256_length_extension_attack.rb +22 -0
- data/sig/sha256_length_extension_attack.rbs +4 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a341dc7126212ab0704ecde4d7324c9178e2b47bcc4303d5e09183e97cc195d8
|
4
|
+
data.tar.gz: 3a2a374eb7ee0f4df04515fdc9f1252b0be034fafe8dd373ed043a4478d46a20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72c8476815d1363430422d6ed97d8e60790e4961fe82a035ffc274aeaf27afe580cd2d7a0e274b84b2208c88465027ac52335a8fb77558920f938915f4d04263
|
7
|
+
data.tar.gz: 7c8ac0bc4ec48fdd99ee180bedd4128db142c12f864243bc41b1c837cb47559fd164b25377c7718619f9ae4919a2d94a5c606c6b25a859773a6783a5826b83e6
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at lby89757@hotmai.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sha256_length_extension_attack (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
base64 (0.1.1)
|
11
|
+
json (2.6.3)
|
12
|
+
language_server-protocol (3.17.0.3)
|
13
|
+
lint_roller (1.1.0)
|
14
|
+
minitest (5.20.0)
|
15
|
+
parallel (1.23.0)
|
16
|
+
parser (3.2.2.3)
|
17
|
+
ast (~> 2.4.1)
|
18
|
+
racc
|
19
|
+
racc (1.7.1)
|
20
|
+
rainbow (3.1.1)
|
21
|
+
rake (13.0.6)
|
22
|
+
regexp_parser (2.8.1)
|
23
|
+
rexml (3.2.6)
|
24
|
+
rubocop (1.56.3)
|
25
|
+
base64 (~> 0.1.1)
|
26
|
+
json (~> 2.3)
|
27
|
+
language_server-protocol (>= 3.17.0)
|
28
|
+
parallel (~> 1.10)
|
29
|
+
parser (>= 3.2.2.3)
|
30
|
+
rainbow (>= 2.2.2, < 4.0)
|
31
|
+
regexp_parser (>= 1.8, < 3.0)
|
32
|
+
rexml (>= 3.2.5, < 4.0)
|
33
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
34
|
+
ruby-progressbar (~> 1.7)
|
35
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
36
|
+
rubocop-ast (1.29.0)
|
37
|
+
parser (>= 3.2.1.0)
|
38
|
+
rubocop-performance (1.19.1)
|
39
|
+
rubocop (>= 1.7.0, < 2.0)
|
40
|
+
rubocop-ast (>= 0.4.0)
|
41
|
+
ruby-progressbar (1.13.0)
|
42
|
+
standard (1.31.1)
|
43
|
+
language_server-protocol (~> 3.17.0.2)
|
44
|
+
lint_roller (~> 1.0)
|
45
|
+
rubocop (~> 1.56.2)
|
46
|
+
standard-custom (~> 1.0.0)
|
47
|
+
standard-performance (~> 1.2)
|
48
|
+
standard-custom (1.0.2)
|
49
|
+
lint_roller (~> 1.0)
|
50
|
+
rubocop (~> 1.50)
|
51
|
+
standard-performance (1.2.0)
|
52
|
+
lint_roller (~> 1.1)
|
53
|
+
rubocop-performance (~> 1.19.0)
|
54
|
+
unicode-display_width (2.4.2)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
x86_64-darwin-22
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
minitest (~> 5.0)
|
61
|
+
rake (~> 13.0)
|
62
|
+
sha256_length_extension_attack!
|
63
|
+
standard (~> 1.3)
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.4.13
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Bran Liang
|
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,43 @@
|
|
1
|
+
# SHA-256 Length Extension Attack Demo
|
2
|
+
|
3
|
+
This is a simple Ruby library designed to demonstrate a [length extension attack](https://en.wikipedia.org/wiki/Length_extension_attack) on the SHA-256 hash algorithm.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```shell
|
8
|
+
gem install sha256_length_extension_attack
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
The original hash is calculated using the following process:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
secret = "SOMETHING_YOU_DONT_KNOW"
|
16
|
+
Digest::SHA256.hexdigest(secret + "::" + "name=bran")
|
17
|
+
```
|
18
|
+
|
19
|
+
The original hash value is: `10b8813fba3378e9cb6ecec95ab471cea46b09eb93607d0851e07e89390e4758`.
|
20
|
+
|
21
|
+
Now, let's say you want to tamper with the message and add an additional query parameter `admin=true` at the end. Since you don't know the exact content of the secret, you cannot generate the new hash directly. However, it's possible to guess the length of the password, and in the worst case, you may need to try several possible lengths. Let's assume you now know that the length of the password is `10`, making the original message byte size `21`. We'll call this value `original_bytesize`. You can use the following method to generate a new hash, including the original message, even though you still don't know the secret:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
suffix, hash = Sha256LengthExtensionAttack.generate(
|
25
|
+
"admin=true",
|
26
|
+
original_hash: "10b8813fba3378e9cb6ecec95ab471cea46b09eb93607d0851e07e89390e4758",
|
27
|
+
original_bytesize: 21
|
28
|
+
)
|
29
|
+
```
|
30
|
+
|
31
|
+
If you inspect the `suffix` variable, you'll notice that it contains a sequence of bits before our targeted content. These bits are typically referred to as padding when hashing normally. However, in this case, we treat them as part of the normal message.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xA8&admin=true
|
35
|
+
```
|
36
|
+
|
37
|
+
The `hash` variable contains the resulting hash value, which you would normally generate using `Digest::SHA256.hexdigest(message)`.
|
38
|
+
|
39
|
+
Congratulations, you have successfully completed a SHA-256 length extension attack!
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
This library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "standard/rake"
|
13
|
+
|
14
|
+
task default: %i[test standard]
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "sha256_length_extension_attack"
|
4
|
+
require "optparse"
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
parser = OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: sha256_length_extension_attack [options]"
|
9
|
+
opts.on("-m", "--message MESSAGE", "Message that will be attached at the end", :required) do |m|
|
10
|
+
options[:message] = m
|
11
|
+
end
|
12
|
+
opts.on("-o", "--original ORIGINAL", "Original message hash", :required) do |o|
|
13
|
+
options[:original] = o
|
14
|
+
end
|
15
|
+
opts.on("-s", "--size SIZE", "Original message size in byte", :required) do |s|
|
16
|
+
options[:size] = s.to_i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
parser.parse!
|
21
|
+
|
22
|
+
if options[:message].nil? || options[:original].nil? || options[:size].nil?
|
23
|
+
# Show help
|
24
|
+
puts parser.help
|
25
|
+
exit
|
26
|
+
end
|
27
|
+
|
28
|
+
suffix, extended_hash = Sha256LengthExtensionAttack.generate(
|
29
|
+
options[:message],
|
30
|
+
original_hash: options[:original],
|
31
|
+
original_bytesize: options[:size]
|
32
|
+
)
|
33
|
+
|
34
|
+
puts "Suffix: #{suffix.inspect}"
|
35
|
+
puts "Extended hash: #{extended_hash}"
|
@@ -0,0 +1,145 @@
|
|
1
|
+
class Sha256
|
2
|
+
K = [
|
3
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
4
|
+
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
5
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
6
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
7
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
8
|
+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
9
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
10
|
+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
11
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
12
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
13
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
14
|
+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
15
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
16
|
+
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
17
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
18
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
def generate_hash(extended_message, original_hash, total_length_in_bits)
|
22
|
+
# 1. Initialize hash values
|
23
|
+
h = original_hash.scan(/.{8}/).map { |chunk| chunk.to_i(16) }
|
24
|
+
|
25
|
+
# 2. Pre-processing
|
26
|
+
message = padding(extended_message, total_length_in_bits)
|
27
|
+
|
28
|
+
# 3. Process the message in successive 512-bit chunks
|
29
|
+
split_message_to_blocks(message).each do |message_block|
|
30
|
+
h = process_message_block(message_block, h)
|
31
|
+
end
|
32
|
+
|
33
|
+
# 4. Produce the final hash value (big-endian)
|
34
|
+
h.map { |hi| hi.to_s(16).rjust(8, "0") }.join
|
35
|
+
end
|
36
|
+
|
37
|
+
def generate_padding(length)
|
38
|
+
p = padding("x" * (length / 8), length)
|
39
|
+
p[length / 8..-1]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# CH( x, y, z) = (x AND y) XOR ( (NOT x) AND z)
|
45
|
+
def ch(x, y, z)
|
46
|
+
(x & y) ^ ((~x) & z)
|
47
|
+
end
|
48
|
+
|
49
|
+
# MAJ( x, y, z) = (x AND y) XOR (x AND z) XOR (y AND z)
|
50
|
+
def maj(x, y, z)
|
51
|
+
(x & y) ^ (x & z) ^ (y & z)
|
52
|
+
end
|
53
|
+
|
54
|
+
# ROTR( x, n) = (x >> n) OR (x << (32 - n))
|
55
|
+
def rotr(x, n)
|
56
|
+
(x >> n) | (x << (32 - n))
|
57
|
+
end
|
58
|
+
|
59
|
+
# SHR( x, n) = x >> n
|
60
|
+
def shr(x, n)
|
61
|
+
x >> n
|
62
|
+
end
|
63
|
+
|
64
|
+
# BSIG0(x) = ROTR^2(x) XOR ROTR^13(x) XOR ROTR^22(x)
|
65
|
+
def bsig0(x)
|
66
|
+
rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)
|
67
|
+
end
|
68
|
+
|
69
|
+
# BSIG1(x) = ROTR^6(x) XOR ROTR^11(x) XOR ROTR^25(x)
|
70
|
+
def bsig1(x)
|
71
|
+
rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)
|
72
|
+
end
|
73
|
+
|
74
|
+
# SSIG0(x) = ROTR^7(x) XOR ROTR^18(x) XOR SHR^3(x)
|
75
|
+
def ssig0(x)
|
76
|
+
rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3)
|
77
|
+
end
|
78
|
+
|
79
|
+
# SSIG1(x) = ROTR^17(x) XOR ROTR^19(x) XOR SHR^10(x)
|
80
|
+
def ssig1(x)
|
81
|
+
rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10)
|
82
|
+
end
|
83
|
+
|
84
|
+
def padding(message, length_in_bits)
|
85
|
+
message = message.dup
|
86
|
+
# 1. append "1" bit to message
|
87
|
+
message << [0x80].pack("C")
|
88
|
+
|
89
|
+
# 2. append "0" bits until message length in bits ≡ 448 (mod 512)
|
90
|
+
# 448 = 56 * 8
|
91
|
+
while message.bytesize % 64 != 56
|
92
|
+
message << [0x00].pack("C")
|
93
|
+
end
|
94
|
+
|
95
|
+
# 3. append length of message in bits as 64-bit big-endian integer
|
96
|
+
message << [length_in_bits].pack("Q>")
|
97
|
+
|
98
|
+
message
|
99
|
+
end
|
100
|
+
|
101
|
+
# Split message into 512-bit blocks
|
102
|
+
def split_message_to_blocks(message)
|
103
|
+
message.bytes.each_slice(64).map { |block| block.pack("C*") }
|
104
|
+
end
|
105
|
+
|
106
|
+
def process_message_block(message_block, hash)
|
107
|
+
# 1. Prepare the message schedule
|
108
|
+
w = []
|
109
|
+
(0..15).each do |t|
|
110
|
+
w[t] = message_block[t * 4, 4].unpack1("N")
|
111
|
+
end
|
112
|
+
(16..63).each do |t|
|
113
|
+
w[t] = (ssig1(w[t - 2]) + w[t - 7] + ssig0(w[t - 15]) + w[t - 16]) % 2**32
|
114
|
+
end
|
115
|
+
|
116
|
+
# 2. Initialize the eight working variables with the (i-1)st hash value
|
117
|
+
a, b, c, d, e, f, g, h = hash
|
118
|
+
|
119
|
+
# 3. Perform the main hash computation
|
120
|
+
(0..63).each do |t|
|
121
|
+
t1 = (h + bsig1(e) + ch(e, f, g) + K[t] + w[t]) % 2**32
|
122
|
+
t2 = (bsig0(a) + maj(a, b, c)) % 2**32
|
123
|
+
h = g
|
124
|
+
g = f
|
125
|
+
f = e
|
126
|
+
e = (d + t1) % 2**32
|
127
|
+
d = c
|
128
|
+
c = b
|
129
|
+
b = a
|
130
|
+
a = (t1 + t2) % 2**32
|
131
|
+
end
|
132
|
+
|
133
|
+
# 4. Compute the intermediate hash value
|
134
|
+
[
|
135
|
+
(a + hash[0]) % 2**32,
|
136
|
+
(b + hash[1]) % 2**32,
|
137
|
+
(c + hash[2]) % 2**32,
|
138
|
+
(d + hash[3]) % 2**32,
|
139
|
+
(e + hash[4]) % 2**32,
|
140
|
+
(f + hash[5]) % 2**32,
|
141
|
+
(g + hash[6]) % 2**32,
|
142
|
+
(h + hash[7]) % 2**32,
|
143
|
+
]
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sha256_length_extension_attack/version"
|
4
|
+
require_relative "sha256_length_extension_attack/sha256"
|
5
|
+
require "digest"
|
6
|
+
|
7
|
+
module Sha256LengthExtensionAttack
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def generate(extension_message, original_hash:, original_bytesize:)
|
12
|
+
original_padding = original_bytesize == 0 ? "" : Sha256.new.generate_padding(original_bytesize * 8)
|
13
|
+
total_bitsize = (original_bytesize + original_padding.bytesize + extension_message.bytesize) * 8
|
14
|
+
extended_hash = Sha256.new.generate_hash(
|
15
|
+
extension_message,
|
16
|
+
original_hash,
|
17
|
+
total_bitsize
|
18
|
+
)
|
19
|
+
[original_padding + extension_message, extended_hash]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sha256_length_extension_attack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bran Liang
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- lby89757@hotmail.com
|
16
|
+
executables:
|
17
|
+
- sha256_length_extension_attack
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".standard.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- exe/sha256_length_extension_attack
|
30
|
+
- lib/sha256_length_extension_attack.rb
|
31
|
+
- lib/sha256_length_extension_attack/sha256.rb
|
32
|
+
- lib/sha256_length_extension_attack/version.rb
|
33
|
+
- sig/sha256_length_extension_attack.rbs
|
34
|
+
homepage:
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
allowed_push_host: https://rubygems.org
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.6.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.4.13
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: SHA256 Hash length extension attack in Ruby.
|
58
|
+
test_files: []
|