|
| 1 | +package at.favre.lib.crypto.bcrypt.benchmark; |
| 2 | + |
| 3 | +import at.favre.lib.bytes.BinaryToTextEncoding; |
| 4 | +import at.favre.lib.bytes.Bytes; |
| 5 | +import at.favre.lib.crypto.bcrypt.BCrypt; |
| 6 | +import com.github.fzakaria.ascii85.Ascii85; |
| 7 | +import org.openjdk.jmh.annotations.*; |
| 8 | + |
| 9 | +import java.nio.ByteOrder; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +@SuppressWarnings("CheckStyle") |
| 14 | +@State(Scope.Thread) |
| 15 | +@Fork(1) |
| 16 | +@Warmup(iterations = 2, time = 4) |
| 17 | +@Measurement(iterations = 3, time = 12) |
| 18 | +@BenchmarkMode(Mode.AverageTime) |
| 19 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 20 | +//CHECKSTYLE.OFF |
| 21 | +public class BcryptBenchmark { |
| 22 | + |
| 23 | + private AbstractBcrypt favreBcrypt; |
| 24 | + private AbstractBcrypt jBcrypt; |
| 25 | + private AbstractBcrypt bcBcrypt; |
| 26 | + private BinaryToTextEncoding.Encoder ascii85 = new BinaryToTextEncoding.Encoder() { |
| 27 | + @Override |
| 28 | + public String encode(byte[] bytes, ByteOrder byteOrder) { |
| 29 | + return Ascii85.encode(bytes); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + @Param({"10", "12"}) |
| 34 | + private int cost; |
| 35 | + private byte[] pw; |
| 36 | + |
| 37 | + @Setup(Level.Trial) |
| 38 | + public void setupBenchmark() { |
| 39 | + favreBcrypt = new FavreBcrypt(); |
| 40 | + jBcrypt = new JBcrypt(); |
| 41 | + bcBcrypt = new BC(); |
| 42 | + } |
| 43 | + |
| 44 | + @Setup(Level.Invocation) |
| 45 | + public void setup() { |
| 46 | + pw = Bytes.random(36).encode(ascii85).getBytes(StandardCharsets.UTF_8); |
| 47 | + } |
| 48 | + |
| 49 | + @Benchmark |
| 50 | + public byte[] benchmarkFavreBcrypt() { |
| 51 | + return benchmark(favreBcrypt, cost, pw); |
| 52 | + } |
| 53 | + |
| 54 | + //@Benchmark |
| 55 | + public byte[] benchmarkJBcrypt() { |
| 56 | + return benchmark(jBcrypt, cost, pw); |
| 57 | + } |
| 58 | + |
| 59 | + //@Benchmark |
| 60 | + public byte[] benchmarkBcBcrypt() { |
| 61 | + return benchmark(bcBcrypt, cost, pw); |
| 62 | + } |
| 63 | + |
| 64 | + private byte[] benchmark(AbstractBcrypt bcrypt, int logRounds, byte[] pw) { |
| 65 | + return bcrypt.bcrypt(logRounds, pw); |
| 66 | + } |
| 67 | + |
| 68 | + static final class FavreBcrypt implements AbstractBcrypt { |
| 69 | + @Override |
| 70 | + public byte[] bcrypt(int cost, byte[] password) { |
| 71 | + return BCrypt.withDefaults().hash(cost, password); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static final class JBcrypt implements AbstractBcrypt { |
| 76 | + @Override |
| 77 | + public byte[] bcrypt(int cost, byte[] password) { |
| 78 | + return org.mindrot.jbcrypt.BCrypt.hashpw(new String(password, StandardCharsets.UTF_8), org.mindrot.jbcrypt.BCrypt.gensalt(cost)).getBytes(StandardCharsets.UTF_8); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + static final class BC implements AbstractBcrypt { |
| 83 | + @Override |
| 84 | + public byte[] bcrypt(int cost, byte[] password) { |
| 85 | + return org.bouncycastle.crypto.generators.BCrypt.generate(Bytes.from(password).append((byte) 0).array(), Bytes.random(16).array(), cost); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + interface AbstractBcrypt { |
| 90 | + byte[] bcrypt(int cost, byte[] password); |
| 91 | + } |
| 92 | +} |
| 93 | +//CHECKSTYLE.ON |
0 commit comments