Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ on:
pull_request:

jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- run: cargo fmt --check
- uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --check bcryptlua.lua spec/

spec:
strategy:
matrix:
Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ dev:
test: dev
busted

.PHONY: dev test
# Format Rust (rustfmt) and Lua (stylua) sources in place.
fmt:
cargo fmt
stylua bcryptlua.lua spec/

# Verify formatting without writing; fails if anything is out of style.
fmt-check:
cargo fmt --check
stylua --check bcryptlua.lua spec/

.PHONY: dev test fmt fmt-check
6 changes: 2 additions & 4 deletions bcryptlua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ function bcrypt.hash(password, cost)
error("Password must be a string", 2)
end
if #password > MAX_PASSWORD_BYTES then
error(("Password exceeds bcrypt's 72-byte limit (%d bytes)")
:format(#password), 2)
error(("Password exceeds bcrypt's 72-byte limit (%d bytes)"):format(#password), 2)
end
if cost == nil then
cost = DEFAULT_COST
elseif type(cost) ~= "number" or cost % 1 ~= 0 then
error("Cost must be an integer", 2)
elseif cost < MIN_COST or cost > MAX_COST then
error(("Cost must be between %d and %d, got %d")
:format(MIN_COST, MAX_COST, cost), 2)
error(("Cost must be between %d and %d, got %d"):format(MIN_COST, MAX_COST, cost), 2)
end
return core.hash(password, cost)
end
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2024"
39 changes: 28 additions & 11 deletions spec/bcrypt_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,30 @@ describe("bcrypt.hash", function()
end)

it("rejects cost below 4 and above 31", function()
assert.error_matches(function() bcrypt.hash("x", 3) end, "between 4 and 31")
assert.error_matches(function() bcrypt.hash("x", 32) end, "between 4 and 31")
assert.error_matches(function()
bcrypt.hash("x", 3)
end, "between 4 and 31")
assert.error_matches(function()
bcrypt.hash("x", 32)
end, "between 4 and 31")
end)

it("rejects a non-integer cost", function()
assert.error_matches(function() bcrypt.hash("x", 4.5) end, "integer")
assert.error_matches(function() bcrypt.hash("x", "12") end, "integer")
assert.error_matches(function()
bcrypt.hash("x", 4.5)
end, "integer")
assert.error_matches(function()
bcrypt.hash("x", "12")
end, "integer")
end)

it("rejects a non-string password", function()
assert.error_matches(function() bcrypt.hash(42, FAST) end, "string")
assert.error_matches(function() bcrypt.hash(nil, FAST) end, "string")
assert.error_matches(function()
bcrypt.hash(42, FAST)
end, "string")
assert.error_matches(function()
bcrypt.hash(nil, FAST)
end, "string")
end)
end)

Expand All @@ -83,8 +95,7 @@ describe("bcrypt.verify", function()
it("accepts known hashes from other implementations ($2a$, $2y$)", function()
for _, vector in ipairs(vectors) do
local password, hash = vector[1], vector[2]
assert.is_true(bcrypt.verify(password, hash),
("vector failed: %q"):format(password))
assert.is_true(bcrypt.verify(password, hash), ("vector failed: %q"):format(password))
end
end)

Expand All @@ -103,8 +114,12 @@ describe("bcrypt.verify", function()
end)

it("rejects a non-string password or hash", function()
assert.error_matches(function() bcrypt.verify(42, "hash") end, "string")
assert.error_matches(function() bcrypt.verify("secret", 42) end, "string")
assert.error_matches(function()
bcrypt.verify(42, "hash")
end, "string")
assert.error_matches(function()
bcrypt.verify("secret", 42)
end, "string")
end)
end)

Expand All @@ -122,6 +137,8 @@ describe("bcrypt.cost", function()
end)

it("rejects a non-string hash", function()
assert.error_matches(function() bcrypt.cost(nil) end, "string")
assert.error_matches(function()
bcrypt.cost(nil)
end, "string")
end)
end)
7 changes: 7 additions & 0 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "Never"
Loading