Skip to content

Commit

Permalink
Fix buggy case in all SHA modules + update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekkonot committed Apr 23, 2024
1 parent d9ca40d commit 023b8f8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions modules/sha1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Several performance improvements
- Add `--!native` directive to module to support native codegen in Roblox
- Fixed incorrect hashing result when input had a length of `55 + 64 * n` where `n` was any integer

## Version 1.0.2

Expand Down
8 changes: 6 additions & 2 deletions modules/sha1/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ local function sha1(message: string): (string, { number })
processBlocks(digest, message, 1, messageLen - leftover)
end

-- TODO find a way to do this more eloquently
-- Raise `leftover` to next multiple of 64 so that we can calculate
-- how much padding we need without a branch or loop.
-- The number here is just masking off the last 6 bits.
local nextMultipleOf64 = bit32.band(leftover + 32, 0xffffffc0)

local finalMessage = {
if leftover ~= 0 then string.sub(message, -leftover) else "",
"\x80",
string.rep("\0", 64 - (messageLen + 9) % 64),
string.rep("\0", (nextMultipleOf64 - leftover - 9) % 64),
string.pack(">L", messageLen * 8),
}
local finalBlock = table.concat(finalMessage)
Expand Down
1 change: 1 addition & 0 deletions modules/sha224/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Several performance improvements
- Add `--!native` directive to module to support native codegen in Roblox
- Fixed incorrect hashing result when input had a length of `55 + 64 * n` where `n` was any integer

## Version 1.0.0

Expand Down
7 changes: 6 additions & 1 deletion modules/sha224/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ local function sha224(message: string): (string, { number })
processBlocks(digest, message, 1, messageLen - leftover)
end

-- Raise `leftover` to next multiple of 64 so that we can calculate
-- how much padding we need without a branch or loop.
-- The number here is just masking off the last 6 bits.
local nextMultipleOf64 = bit32.band(leftover + 32, 0xffffffc0)

local finalMessage = {
if leftover ~= 0 then string.sub(message, -leftover) else "",
"\x80",
string.rep("\0", 64 - (messageLen + 9) % 64),
string.rep("\0", (nextMultipleOf64 - leftover - 9) % 64),
string.pack(">L", messageLen * 8),
}
local finalBlock = table.concat(finalMessage)
Expand Down
1 change: 1 addition & 0 deletions modules/sha256/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Several performance improvements
- Add `--!native` directive to module to support native codegen in Roblox
- Fixed incorrect hashing result when input had a length where `length % 55` was `0`

## Version 1.0.0

Expand Down
7 changes: 6 additions & 1 deletion modules/sha256/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ local function sha256(message: string): (string, { number })
processBlocks(digest, message, 1, messageLen - leftover)
end

-- Raise `leftover` to next multiple of 64 so that we can calculate
-- how much padding we need without a branch or loop.
-- The number here is just masking off the last 6 bits.
local nextMultipleOf64 = bit32.band(leftover + 32, 0xffff_ffc0)

local finalMessage = {
if leftover ~= 0 then string.sub(message, -leftover) else "",
"\x80",
string.rep("\0", 64 - (messageLen + 9) % 64),
string.rep("\0", (nextMultipleOf64 - leftover - 9) % 64),
string.pack(">L", messageLen * 8),
}
local finalBlock = table.concat(finalMessage)
Expand Down
1 change: 1 addition & 0 deletions modules/sha384/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Several performance improvements
- Add `--!native` directive to module to support native codegen in Roblox
- Fixed incorrect hashing result when input had a length of `111 + 128 * n` where `n` was any integer

## Version 1.0.0

Expand Down
8 changes: 7 additions & 1 deletion modules/sha384/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,16 @@ local function sha384(message: string): string
if messageLen >= 128 then
processBlocks(digest_front, digest_back, message, 1, messageLen - leftover)
end

-- Raise `leftover` to next multiple of 128 so that we can calculate
-- how much padding we need without a branch or loop.
-- The number here is just masking off the last 6 bits.
local nextMultipleOf128 = bit32.band(leftover + 64, 0xfffff000)

local finalMessage = {
if leftover ~= 0 then string.sub(message, -leftover) else "",
"\x80",
string.rep("\0", 128 - (messageLen + 17) % 128),
string.rep("\0", (nextMultipleOf128 - leftover - 17) % 128),
string.pack(">L", messageLen * 8 / 2 ^ 32),
string.pack(">L", bit32.bor(messageLen * 8)),
}
Expand Down
1 change: 1 addition & 0 deletions modules/sha512/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Several performance improvements
- Add `--!native` directive to module to support native codegen in Roblox
- Fixed incorrect hashing result when input had a length of `111 + 128 * n` where `n` was any integer

## Version 1.0.0

Expand Down
8 changes: 7 additions & 1 deletion modules/sha512/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,16 @@ local function sha512(message: string): string
if messageLen >= 128 then
processBlocks(digest_front, digest_back, message, 1, messageLen - leftover)
end

-- Raise `leftover` to next multiple of 128 so that we can calculate
-- how much padding we need without a branch or loop.
-- The number here is just masking off the last 6 bits.
local nextMultipleOf128 = bit32.band(leftover + 64, 0xfffff000)

local finalMessage = {
if leftover ~= 0 then string.sub(message, -leftover) else "",
"\x80",
string.rep("\0", 128 - (messageLen + 17) % 128),
string.rep("\0", (nextMultipleOf128 - leftover - 17) % 128),
string.pack(">L", messageLen * 8 / 2 ^ 32),
string.pack(">L", messageLen * 8 % 2 ^ 32),
}
Expand Down

0 comments on commit 023b8f8

Please sign in to comment.