@@ -526,35 +526,47 @@ static long roundsForLogRounds(int log_rounds) {
526
526
* @param safety bit 16 is set when the safety measure is requested
527
527
* @return an array containing the binary hashed password
528
528
*/
529
- private byte [] crypt_raw (byte password [], byte salt [], int log_rounds , boolean sign_ext_bug , int safety ) {
530
- int rounds , i , j ;
529
+ private byte [] crypt_raw (byte password [], byte salt [], int log_rounds , boolean sign_ext_bug , int safety ,
530
+ boolean for_check ) {
531
531
int cdata [] = bf_crypt_ciphertext .clone ();
532
532
int clen = cdata .length ;
533
- byte ret [];
534
533
534
+ long rounds ;
535
535
if (log_rounds < 4 || log_rounds > 31 ) {
536
- throw new IllegalArgumentException ("Bad number of rounds" );
536
+ if (!for_check ) {
537
+ throw new IllegalArgumentException ("Bad number of rounds" );
538
+ }
539
+ if (log_rounds != 0 ) {
540
+ throw new IllegalArgumentException ("Bad number of rounds" );
541
+ }
542
+ rounds = 0 ;
543
+ }
544
+ else {
545
+ rounds = roundsForLogRounds (log_rounds );
546
+ if (rounds < 16 || rounds > Integer .MAX_VALUE ) {
547
+ throw new IllegalArgumentException ("Bad number of rounds" );
548
+ }
537
549
}
538
- rounds = 1 << log_rounds ;
550
+
539
551
if (salt .length != BCRYPT_SALT_LEN ) {
540
552
throw new IllegalArgumentException ("Bad salt length" );
541
553
}
542
554
543
555
init_key ();
544
556
ekskey (salt , password , sign_ext_bug , safety );
545
- for (i = 0 ; i < rounds ; i ++) {
557
+ for (int i = 0 ; i < rounds ; i ++) {
546
558
key (password , sign_ext_bug , safety );
547
559
key (salt , false , safety );
548
560
}
549
561
550
- for (i = 0 ; i < 64 ; i ++) {
551
- for (j = 0 ; j < (clen >> 1 ); j ++) {
562
+ for (int i = 0 ; i < 64 ; i ++) {
563
+ for (int j = 0 ; j < (clen >> 1 ); j ++) {
552
564
encipher (cdata , j << 1 );
553
565
}
554
566
}
555
567
556
- ret = new byte [clen * 4 ];
557
- for (i = 0 , j = 0 ; i < clen ; i ++) {
568
+ byte [] ret = new byte [clen * 4 ];
569
+ for (int i = 0 , j = 0 ; i < clen ; i ++) {
558
570
ret [j ++] = (byte ) ((cdata [i ] >> 24 ) & 0xff );
559
571
ret [j ++] = (byte ) ((cdata [i ] >> 16 ) & 0xff );
560
572
ret [j ++] = (byte ) ((cdata [i ] >> 8 ) & 0xff );
@@ -563,6 +575,10 @@ private byte[] crypt_raw(byte password[], byte salt[], int log_rounds, boolean s
563
575
return ret ;
564
576
}
565
577
578
+ private static String hashpwforcheck (byte [] passwordb , String salt ) {
579
+ return hashpw (passwordb , salt , true );
580
+ }
581
+
566
582
/**
567
583
* Hash a password using the OpenBSD bcrypt scheme
568
584
* @param password the password to hash
@@ -584,6 +600,10 @@ public static String hashpw(String password, String salt) {
584
600
* @return the hashed password
585
601
*/
586
602
public static String hashpw (byte passwordb [], String salt ) {
603
+ return hashpw (passwordb , salt , false );
604
+ }
605
+
606
+ private static String hashpw (byte passwordb [], String salt , boolean for_check ) {
587
607
BCrypt B ;
588
608
String real_salt ;
589
609
byte saltb [], hashed [];
@@ -633,7 +653,7 @@ public static String hashpw(byte passwordb[], String salt) {
633
653
}
634
654
635
655
B = new BCrypt ();
636
- hashed = B .crypt_raw (passwordb , saltb , rounds , minor == 'x' , minor == 'a' ? 0x10000 : 0 );
656
+ hashed = B .crypt_raw (passwordb , saltb , rounds , minor == 'x' , minor == 'a' ? 0x10000 : 0 , for_check );
637
657
638
658
rs .append ("$2" );
639
659
if (minor >= 'a' ) {
@@ -740,7 +760,8 @@ public static String gensalt() {
740
760
* @return true if the passwords match, false otherwise
741
761
*/
742
762
public static boolean checkpw (String plaintext , String hashed ) {
743
- return equalsNoEarlyReturn (hashed , hashpw (plaintext , hashed ));
763
+ byte [] passwordb = plaintext .getBytes (StandardCharsets .UTF_8 );
764
+ return equalsNoEarlyReturn (hashed , hashpwforcheck (passwordb , hashed ));
744
765
}
745
766
746
767
/**
@@ -751,7 +772,7 @@ public static boolean checkpw(String plaintext, String hashed) {
751
772
* @since 5.3
752
773
*/
753
774
public static boolean checkpw (byte [] passwordb , String hashed ) {
754
- return equalsNoEarlyReturn (hashed , hashpw (passwordb , hashed ));
775
+ return equalsNoEarlyReturn (hashed , hashpwforcheck (passwordb , hashed ));
755
776
}
756
777
757
778
static boolean equalsNoEarlyReturn (String a , String b ) {
0 commit comments