Skip to content

Comments

Add BCrypt and Argon2 password handling to crypto module#577

Merged
daneshk merged 42 commits intoballerina-platform:masterfrom
randilt:implement-password-handling-bcrypt
Jan 20, 2025
Merged

Add BCrypt and Argon2 password handling to crypto module#577
daneshk merged 42 commits intoballerina-platform:masterfrom
randilt:implement-password-handling-bcrypt

Conversation

@randilt
Copy link
Contributor

@randilt randilt commented Jan 10, 2025

Purpose

This PR introduces implementation for BCrypt and Argon2id password hashing in Ballerina Crypto Module. It provides functions for securely hashing passwords, verifying them, and generating salts with customizable parameters. The support for both BCrypt and Argon2id allows for flexible and secure password management with different algorithms and configurable options like work factor (for BCrypt) and iterations, memory, and parallelism (for Argon2id).
Resolves: #2744

Fixes: ballerina-platform/ballerina-library#2441
Fixes: ballerina-platform/ballerina-library#2744

Examples

// Hashing a password with BCrypt
string password = "mySecurePassword123";
string|crypto:Error hash = crypto:hashPassword(password);

// Verifying a BCrypt hashed password
string password = "mySecurePassword123";
string hashedPassword = "$2a$12$LQV3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewYpwBAM7RHF.H9m";
boolean|crypto:Error matches = crypto:verifyPassword(password, hashedPassword);

// Generating a BCrypt salt
string|crypto:Error salt = crypto:generateSalt(14);

// Hashing a password with Argon2id
string password = "mySecurePassword123";
string|crypto:Error hashArgon2 = crypto:hashPasswordArgon2(password);

// Verifying an Argon2id hashed password
string password = "mySecurePassword123";
string hashedPasswordArgon2 = "$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$hash";
boolean|crypto:Error matchesArgon2 = crypto:verifyPasswordArgon2(password, hashedPasswordArgon2);

// Generating an Argon2id salt
string|crypto:Error saltArgon2 = crypto:generateSaltArgon2(4, 131072, 8);

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

@daneshk
Copy link
Member

daneshk commented Jan 13, 2025

@randilt Thank you for your contribution.

@MohamedSabthar @DimuthuMadushan Please review the new APIs.

@randilt randilt requested a review from daneshk January 13, 2025 17:58
randilt and others added 3 commits January 15, 2025 00:07
…sword.java

Co-authored-by: Danesh Kuruppu <daneshk@users.noreply.github.com>
…swordArgon2.java

Co-authored-by: Danesh Kuruppu <daneshk@users.noreply.github.com>
…java

Co-authored-by: Danesh Kuruppu <daneshk@users.noreply.github.com>
@randilt randilt requested a review from daneshk January 14, 2025 18:40
Copy link

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for this "drive-by" review. Hopefully these comments are useful, but I am not a project member so feel free to consider these comments at most as suggestions. I don't want to disrupt this review process.

@randilt randilt dismissed stale reviews from daneshk and ThisaruGuruge via 20bbda9 January 19, 2025 05:43
@randilt
Copy link
Contributor Author

randilt commented Jan 19, 2025

@ThisaruGuruge @daneshk Please review the newly added changes.

Added security fixes and improvements suggested by @Marcono1234

Thanks for the suggestions! @Marcono1234

@daneshk
Copy link
Member

daneshk commented Jan 19, 2025

@randilt One final request. Could you please add a proposal for this new API in the docs/proposals directory and link the issue? The example proposal can be found here

@randilt
Copy link
Contributor Author

randilt commented Jan 20, 2025

@randilt One final request. Could you please add a proposal for this new API in the docs/proposals directory and link the issue? The example proposal can be found here

@daneshk I have added the proposal here please check

Co-authored-by: Danesh Kuruppu <daneshk@users.noreply.github.com>
@randilt randilt requested a review from daneshk January 20, 2025 04:21
@sonarqubecloud
Copy link

Copy link
Member

@daneshk daneshk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@ThisaruGuruge ThisaruGuruge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@daneshk
Copy link
Member

daneshk commented Jan 20, 2025

@randilt, thank you very much for your contribution. It will be included in our next release.

@daneshk daneshk merged commit 6c48143 into ballerina-platform:master Jan 20, 2025
6 checks passed
@randilt
Copy link
Contributor Author

randilt commented Jan 20, 2025

@randilt, thank you very much for your contribution. It will be included in our next release.

I'm glad to contribute! Thank you all for your suggestions and support. I learned a lot during this PR. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add library to securely hash passwords (like bcrypt) Add Bcrypt Support in Crypto Standard Library

6 participants