@@ -616,7 +616,7 @@ public static string HashPassword(string inputKey, string salt, bool enhancedEnt
616616
617617 // Determine the starting offset and validate the salt
618618 int startingOffset ;
619- char minor = ( char ) 0 ;
619+ char bcryptMinorRevision = ( char ) 0 ;
620620 if ( salt [ 0 ] != '$' || salt [ 1 ] != '2' )
621621 {
622622 throw new SaltParseException ( "Invalid salt version" ) ;
@@ -627,8 +627,8 @@ public static string HashPassword(string inputKey, string salt, bool enhancedEnt
627627 }
628628 else
629629 {
630- minor = salt [ 2 ] ;
631- if ( minor != 'a' && minor != 'b' && minor != 'x' && minor != 'y' || salt [ 3 ] != '$' )
630+ bcryptMinorRevision = salt [ 2 ] ;
631+ if ( bcryptMinorRevision != 'a' && bcryptMinorRevision != 'b' && bcryptMinorRevision != 'x' && bcryptMinorRevision != 'y' || salt [ 3 ] != '$' )
632632 {
633633 throw new SaltParseException ( "Invalid salt revision" ) ;
634634 }
@@ -652,7 +652,7 @@ public static string HashPassword(string inputKey, string salt, bool enhancedEnt
652652
653653 string extractedSalt = salt . Substring ( startingOffset + 3 , 22 ) ;
654654
655- byte [ ] inputBytes = SafeUTF8 . GetBytes ( inputKey + ( minor >= 'a' ? Nul : EmptyString ) ) ;
655+ byte [ ] inputBytes = SafeUTF8 . GetBytes ( inputKey + ( bcryptMinorRevision >= 'a' ? Nul : EmptyString ) ) ;
656656
657657 if ( enhancedEntropy )
658658 {
@@ -666,12 +666,7 @@ public static string HashPassword(string inputKey, string salt, bool enhancedEnt
666666 byte [ ] hashed = bCrypt . CryptRaw ( inputBytes , saltBytes , workFactor ) ;
667667
668668 // Generate result string
669- StringBuilder result = new StringBuilder ( ) ;
670- result . AppendFormat ( "$2{1}${0:00}$" , workFactor , minor ) ;
671- result . Append ( EncodeBase64 ( saltBytes , saltBytes . Length ) ) ;
672- result . Append ( EncodeBase64 ( hashed , ( BfCryptCiphertext . Length * 4 ) - 1 ) ) ;
673-
674- return result . ToString ( ) ;
669+ return $ "$2{ bcryptMinorRevision } ${ workFactor : 00} ${ EncodeBase64 ( saltBytes , saltBytes . Length ) } { EncodeBase64 ( hashed , ( BfCryptCiphertext . Length * 4 ) - 1 ) } ";
675670 }
676671
677672 /// <summary>
@@ -729,10 +724,7 @@ public static string GenerateSalt(int workFactor, char bcryptMinorRevision = Def
729724
730725 RngCsp . GetBytes ( saltBytes ) ;
731726
732- StringBuilder result = new StringBuilder ( ) ;
733- result . AppendFormat ( "$2{1}${0:00}$" , workFactor , bcryptMinorRevision ) ;
734- result . Append ( EncodeBase64 ( saltBytes , saltBytes . Length ) ) ;
735- return result . ToString ( ) ;
727+ return $ "$2{ bcryptMinorRevision } ${ workFactor : 00} ${ EncodeBase64 ( saltBytes , saltBytes . Length ) } ";
736728 }
737729
738730
@@ -748,8 +740,7 @@ public static string GenerateSalt(int workFactor, char bcryptMinorRevision = Def
748740 public static bool PasswordNeedsRehash ( string hash , int newMinimumWorkLoad )
749741 {
750742 var hashInfo = InterrogateHash ( hash ) ;
751- int currentWorkLoad ;
752- if ( ! Int32 . TryParse ( hashInfo . WorkFactor , out currentWorkLoad ) )
743+ if ( ! Int32 . TryParse ( hashInfo . WorkFactor , out var currentWorkLoad ) )
753744 {
754745 throw new ArgumentException ( "Work Factor (logrounds) could not be parsed" , nameof ( hash ) ) ;
755746 }
@@ -876,7 +867,7 @@ private static string EncodeBase64(byte[] byteArray, int length)
876867 }
877868
878869 int off = 0 ;
879- StringBuilder rs = new StringBuilder ( ) ;
870+ StringBuilder rs = new StringBuilder ( length ) ;
880871 while ( off < length )
881872 {
882873 int c1 = byteArray [ off ++ ] & 0xff ;
0 commit comments