[JAVA] Encrypt strings with B Crypt quickly

I wrote the code to encrypt the string with BCrypt with the command line argument, so make a note of it. It's supposed to be done quickly without using package management tools such as Maven.

Package download

Manually download the library to use from the site.

Implementation

Store these libraries under lib and implement the following 2 classes. CLBCryptPasswordEncoder.java that encrypts the arguments with BCrypt.

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.apache.commons.logging.LogFactory;

public class CLBCryptPasswordEncoder{
  public static void main(String args[]){

    String textPassword = args[0];

    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    System.out.println(encoder.encode(textPassword));
  }
}

CLBCryptPasswordMatcher.java to check if the plaintext password and the encrypted password match.

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.apache.commons.logging.LogFactory;

public class CLBCryptPasswordMatcher{
  public static void main(String args[]){
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    String textPassword = args[0];
    String encryptedPassword = args[1];

    if (encoder.matches(textPassword, encryptedPassword)) {
      System.out.println("matched");
    } else {
      System.out.println("mismatched");
    }
  }
}

Directory structure

The directory structure is as follows.

compile

$ javac -cp "lib/*" CLBCryptPasswordEncoder.java
$ javac -cp "lib/*" CLBCryptPasswordMatcher.java

A class is created in the current directory.

Run

Perform encryption and verification. Add current directory to classpath.

$ java -cp "lib/*:." CLBCryptPasswordEncoder sonomirai
$2a$10$ALcXPgrpOQKXoIyrgS90huCbgR906LtWrH1dOsZmHtBZdSB19n9Bi
$ java -cp "lib/*:." CLBCryptPasswordMatcher sonomirai '$2a$10$ALcXPgrpOQKXoIyrgS90huCbgR906LtWrH1dOsZmHtBZdSB19n9Bi'
matched
$ java -cp "lib/*:." CLBCryptPasswordMatcher dummypswd '$2a$10$ALcXPgrpOQKXoIyrgS90huCbgR906LtWrH1dOsZmHtBZdSB19n9Bi'
mismatched

I did what I wanted to do like this.

Recommended Posts

Encrypt strings with B Crypt quickly
Encrypt strings with B Crypt quickly
Check performance quickly with irb
Sort strings functionally with java