All Projects → Password4j → Password4j

Password4j / Password4j

Licence: apache-2.0
Password4j is a user-friendly cryptographic library that supports Argon2, Bcrypt, Scrypt, PBKDF2 and various cryptographic hash functions.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Password4j

CryptoKnight
CryptoKnight is a general purpose cryptography desktop app
Stars: ✭ 18 (-85.48%)
Mutual labels:  hashing, scrypt, password, bcrypt
hash-wasm
Lightning fast hash functions using hand-tuned WebAssembly binaries
Stars: ✭ 382 (+208.06%)
Mutual labels:  scrypt, argon2, hash, bcrypt
phc-crypto
Hashing algorithms simplified (supports Argon2, Bcrypt, Scrypt, and PBKDF2)
Stars: ✭ 22 (-82.26%)
Mutual labels:  scrypt, argon2, hash, bcrypt
crypthash-net
CryptHash.NET is a .NET multi-target library to encrypt/decrypt/hash/encode/decode strings and files, with an optional .NET Core multiplatform console utility.
Stars: ✭ 33 (-73.39%)
Mutual labels:  argon2, password, hash, bcrypt
Argon2 Jvm
Argon2 Binding for the JVM
Stars: ✭ 245 (+97.58%)
Mutual labels:  hashing, jvm, password, argon2
Lazysodium Android
An Android implementation of the Libsodium cryptography library. For the lazy dev.
Stars: ✭ 69 (-44.35%)
Mutual labels:  hashing, cryptography, argon2
Simple Scrypt
A convenience library for generating, comparing and inspecting password hashes using the scrypt KDF in Go 🔑
Stars: ✭ 168 (+35.48%)
Mutual labels:  hash, password, scrypt
bcrypt
BCrypt is a password hashing function
Stars: ✭ 138 (+11.29%)
Mutual labels:  password, hash, bcrypt
bookshelf-secure-password
A Bookshelf.js plugin for handling secure passwords
Stars: ✭ 24 (-80.65%)
Mutual labels:  password, hash, bcrypt
BruteForce
A simple brute forcer written in GO for SHA1, SHA256, SHA512, MD5 and bcrypt
Stars: ✭ 49 (-60.48%)
Mutual labels:  password, hash, bcrypt
CppSecurity
C++ Security Library
Stars: ✭ 24 (-80.65%)
Mutual labels:  scrypt, argon2, bcrypt
Node Argon2
Node.js bindings for Argon2 hashing algorithm
Stars: ✭ 1,008 (+712.9%)
Mutual labels:  hashing, password, argon2
Crypto Password
Library for securely hashing passwords
Stars: ✭ 185 (+49.19%)
Mutual labels:  cryptography, bcrypt, scrypt
Scrypt
A .NET implementation of scrypt password hash algorithm.
Stars: ✭ 90 (-27.42%)
Mutual labels:  scrypt, hash, bcrypt
noble-hashes
Audited & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt
Stars: ✭ 213 (+71.77%)
Mutual labels:  hashing, scrypt, hash
Upash
🔒Unified API for password hashing algorithms
Stars: ✭ 484 (+290.32%)
Mutual labels:  hashing, password, bcrypt
Unchained
Secure password hashers for Go compatible with Django
Stars: ✭ 46 (-62.9%)
Mutual labels:  password, bcrypt, argon2
Low Latency Android Ios Linux Windows Tvos Macos Interactive Audio Platform
🇸Superpowered Audio, Networking and Cryptographics SDKs. High performance and cross platform on Android, iOS, macOS, tvOS, Linux, Windows and modern web browsers.
Stars: ✭ 1,121 (+804.03%)
Mutual labels:  hashing, cryptography
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (-2.42%)
Mutual labels:  hash, hashing
Comeonin
Password hashing specification for the Elixir programming language
Stars: ✭ 1,166 (+840.32%)
Mutual labels:  bcrypt, argon2

logo

Build Status Maven Central Java 8 or higher Android 5.0 or higher Awesome

Quality Gate Status Security Rating Reliability Rating Maintainability Rating Coverage

Password4j is a Java user-friendly cryptographic library for hashing and checking passwords with different Key derivation functions (KDFs) and Cryptographic hash functions (CHFs).

Algorithms can be configured programmatically or through a property file in your classpath see Configuration section.

The configurations are mostly dependent on your environment. Password4j delivers a tool that can create a set of optimal parameters based on the system performance and the desired maximum computational time see Performance section.

Hash Verify

The library fully supports Argon2, BCrypt, SCrypt and PBKDF2 and can produce and handle cryptographic salt and pepper.

Documentation

Wiki javadoc

The full documentation can be found here. For a quick start you can follow the instuctions in the README.md.

The javadoc can be found here.

Installation

Password4j runs on Java 8 or higher versions by any vendor. It is supported by Android API 21+ as well.

The artifacts are deployed to Maven Central.

Maven Maven

Add the dependency of the latest version to your pom.xml:

<dependency>
    <groupId>com.password4j</groupId>
    <artifactId>password4j</artifactId>
    <version>1.5.2</version>
</dependency>

Gradle Gradle

Add to your build.gradle module dependencies:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.password4j:password4j:1.5.2'
}

Scala SBT Scala SBT

Add to the managed dependencies of your build.sbt the latest version:

libraryDependencies += "com.password4j" % "password4j" % "1.5.2"

Usage

Password4j provides three main features: password hashing, hash checking and hash updating.

Hash the password

Here it is the easiest way to hash a password with a CHF (BCrypt in this case)

Hash hash = Password.hash(password).withBCrypt();

Salt and pepper may be optionally added to the builder (PBKDF2 in this case):

// PBKDF2 with salt 12 bytes long (randomly generated).
Hash hash = Password.hash(password).addRandomSalt(12).withPBKDF2();

// PBKDF2 with a chosen salt.
Hash hash = Password.hash(password).addSalt(salt).withPBKDF2();

// PBKDF2 with chosen salt and pepper.
Hash hash = Password.hash(password).addSalt(salt).addPepper(pepper).withPBKDF2();

// Custom PBKDF2 (PBKDF2 with HMAC-SHA512, 64000 iterations and 512bit length).
Hash hash = Password.hash(password).with(PBKDF2Function.getInstance(Hmac.SHA512, 64000, 512));

The same structure can be adopted for the other CHFs, not just for PBKDF2.

Verify the hash

With the same ease you can verify the hash:

boolean verified = Password.check(password, hash).withBCrypt();

Salt and pepper may be optionally added to the builder (PBKDF2 in this case):

// Verify with PBKDF2.
boolean verification = Password.check(password, hash).withPBKDF2();

// Verify with PBKDF2 and manually provided salt.
boolean verification = Password.check(password, hash).addSalt(salt).withPBKDF2();

// Verify with PBKDF2 and manually provided salt and pepper.
boolean verification = Password.check(password, hash).addSalt(salt).addPepper(pepper).withPBKDF2();

The same structure can be adopted for the other algorithms, not just for PBKDF2. Take in account that Argon2, BCrypt and SCrypt store the salt inside the hash, so the addSalt() method is not needed.

// Verify with Argon2, reads the salt from the given hash.
boolean verification = Password.check(password, hash).withArgon2();

Some algorithms encode into the hash the parameters that were used to compute that hash, notably BCrypt, SCrypt, and Argon2. When checking a hash, you can use the parameters from the hash rather than Password4j's configured defaults.

// Verify with Argon2, reads the salt and parameters from the given hash.
boolean verification = Password.check(password, hash)..with(Argon2Function.getInstanceFromHash(hash)));

Update the hash

When a configuration is not considered anymore secure you can refresh the hash with a more modern algorithm like this:

// Reads the latest configurations in your psw4j.properties
HashUpdate update = Password.check(password, hash).update().withBCrypt();

if(update.isVerified())
{
    Hash newHash = update.getHash();
}

Or if you want to switch from a CHF to another one:

PBKDF2Function pbkdf2 = AlgorithmFinder.getPBKDF2Instance();
HashUpdate update = Password.check(password, hash).update().withSCrypt(pbkdf2);

if(update.isVerified())
{
    Hash newHash = update.getHash();
}

Unsecure Algorithms

Many systems may still use unsecure algorithms for storing the passwords, like MD5 or SHA-256. You can easily migrate to stronger algorithms with Password4j

MessageDigestFunction md = MessageDigestFunction.getInstance("SHA-256");
HashUpdate update = Password.check(password, hash).update().withSCrypt(md);

if(update.isVerified())
{
    Hash newHash = update.getHash();
}

List of supported algorithms

Key derivation Functions Since Notes
PBKDF2 1.0.0 Depending on the Security Services your JVM provides
BCrypt 1.0.0
SCrypt 1.0.0
Argon2 1.5.0
Cryptographic Hash Functions Since Notes
MD Family 1.4.0
SHA1 Family 1.4.0
SHA2 Family 1.4.0
SHA3 FAmily 1.4.0 Depending on the Security Providers your JVM provides

Security of Strings

Strings are immutable objects and once stored in memory you cannot erase them until Garbage Collection. It is always recommended to use char[] instead of String for storing passwords(where possible - If we're talking of a web application, most web containers will pass the password into the HttpServletRequest object in plaintext as String).

An attacker that is able to dump the memory could read the password before you use it as input for Password4j; even if it is read after its usage, it is not guaranteed when the garbage collection occurs: that means that the password may be stored in memory indefinitely and its value cannot be erased.

For this reason Password4j provides a SecureString class that alleviates this problem. The provided char[] is wrapped around SecureString and it is never converted into a String during the process.

You can erase the underlying char[] with clear() method.

SecureString secure = new SecureString(new char[]{...});

Password.hash(secure).withBCrypt();
Password.check(secure, hash).withBCrypt();

secure.clear();
// At this point the underlying char[] = {\0, \0, \0, ...}

In addition to this, you may want to clean the original char[]. With the following code even the source is zeroed:

char[] password = {...}
SecureString secure = new SecureString(password, true);

// At this point password = {\0, \0, \0, ...}

The pepper can be expressed as SecureString as well.

Using SecureString or char[] does not completely defend you from attacks: the Garbage Collector constantly copies objects from the from space to the to space and ereasing the original char[] does not erase its copies; moreover it is never guaranteed that clear() is applied before the garbage collection. For these reasons the usage of SecureString or char[] just reduces the window of opportunities for an attacker.

Configuration

Password4j makes available a portable way to configure the library.

With the property file psw4j.properties put in your classpath, you can define the parameters of all the supported CHFs or just the CHF(s) you need. Alternatively you can specify a custom path with the system property -Dpsw4j.configuration

java -Dpsw4j.configuration=/my/path/to/some.properties ...

Here's a basic configuration (please do not use it in production, but instead start a benchmark session in your target environmentsee Performance section)

### Argon2
hash.argon2.memory=4096
hash.argon2.iterations=20
hash.argon2.length=128
hash.argon2.parallelism=4
hash.argon2.type=id


### BCrypt
hash.bcrypt.minor=b
# logarithmic cost (cost = 2^12)
hash.bcrypt.rounds=12


### SCrypt
# N
hash.scrypt.workfactor=16384
# r
hash.scrypt.resources=16
# p
hash.scrypt.parallelization=1
# length
hash.scrypt.derivedKeyLength=64

### PBKDF2
# with HMAC-SHA256
hash.pbkdf2.algorithm=SHA256
# 64000 iterations
hash.pbkdf2.iterations=64000
# derived key of 256bit 
hash.pbkdf2.length=256


### Legacy MessageDisgest
# algorithm
hash.md.algorithm=SHA-512
# append/prepend salt
hash.md.salt.option=append

Additionally you can define here your shared pepper

global.pepper=AlicePepper

and use it like this

// Hash
Password.hash("password").addPepper().withSCrypt();

// Verify
Password.check("password", "hash").addPepper().withSCrypt();

SecureRandom may be instantiated and used through SecureRandom.getInstanceStrong() to generate salts and peppers.

global.random.strong=true

but make sure that your JVM supports it and it points to a non-blocking source of entropy, otherwise you may experience huge performance dropssee SecureRandom.

Performance

This tool must be used in the target system because performances may vary on different environments.

Password4j is delivered with a tool that helps the developers to choose the right parameters for a specific CHF.

The class SystemChecker can be used to find these optimal values.

In the wiki you can find how to configure PBKDF2, bcrypt, scrypt and Argon2 depending on your responsiveness requirements.

Contributing

GitHub issues GitHub closed issues

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

SemVer 2.0.0

We use SemVer for versioning.

For the versions available, see the releases on this repository.

Authors

GitHub contributors

  • David Bertoldi - Main Maintainer - firaja

See also the list of contributors who participated in this project.

License

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

Changelog

GitHub Release Date

See the CHANGELOG.md file for a more detailed description of each release.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].