### Hash and verify a password with BCrypt.HashPassword and BCrypt.Verify Source: https://github.com/bcryptnet/bcrypt.net/blob/main/readme.md Use BCrypt.HashPassword to generate a salted hash with the default work factor of 11, and BCrypt.Verify to check a password against the hash. The library generates the salt automatically; supplying your own salt is discouraged except for advanced cross-platform needs. File-scoped namespaces are shown, so add curly brackets if needed. ```csharp namespace MyDotNetProject; using BCryptNet; // Hash a password string passwordHash = BCrypt.HashPassword("my password"); // Verify a password if(BCrypt.Verify("my password", passwordHash)) { // Password is correct } ``` -------------------------------- ### Benchmark to Find Appropriate Cost Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/salts.md Console benchmark that starts at cost 16 (65,536 iterations) and decrements until the hashing time falls below the 100 ms target, then prints the appropriate cost. The default work factor is 11; minimum is 4 and maximum is 31. ```csharp var cost = 16; var timeTarget = 100; // Milliseconds long timeTaken; do { var sw = Stopwatch.StartNew(); BCrypt.HashPassword("RwiKnN>9xg3*C)1AZl.)y8f_:GCz,vt3T]PI", workFactor: cost); sw.Stop(); timeTaken = sw.ElapsedMilliseconds; cost -= 1; } while ((timeTaken) >= timeTarget); Console.WriteLine("Appropriate Cost Found: " + (cost + 1)); Console.ReadLine(); ``` -------------------------------- ### Laravel route using Hash::make with rounds option Source: https://github.com/bcryptnet/bcrypt.net/blob/main/assets/Laravel11Test.txt Defines a GET route that hashes the string 'password' using Laravel's Hash facade with a bcrypt cost of 12 rounds. The resulting hash is passed to a view named 'playground' as the 'title' variable. Requires the Hash facade import and a Laravel application context. ```php 12, ]); return view('playground', [ 'title' => $hashed ]); }); ``` -------------------------------- ### Clone the repository with git Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/building.md Clones the Bcrypt.Net repository from GitHub using SSH. ```bash git clone git@github.com:BcryptNet/bcrypt.net.git ``` -------------------------------- ### Build the library with dotnet build Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/building.md Builds the library from the root of the repository using the .NET Core SDK. ```bash dotnet build ``` -------------------------------- ### Run benchmarks with dotnet run Source: https://github.com/bcryptnet/bcrypt.net/blob/main/benchmarks/Method-Benchmarks/readme.md Command to run the benchmarks in Release mode for net9.0 on x64. Change the framework and remove netfwk runtimes if running on Linux. ```bash dotnet run -c Release -f net9.0 -- --runtimes net9.0 --platform x64 ``` -------------------------------- ### Run benchmarks with filter and stopOnFirstError Source: https://github.com/bcryptnet/bcrypt.net/blob/main/benchmarks/Method-Benchmarks/readme.md Command to run benchmarks with a filter and stop on first error. Change the framework and remove netfwk runtimes if running on Linux. ```bash dotnet run -c Release -f net9.0 -- --runtimes net9.0 --filter * --stopOnFirstError --platform x64 ``` -------------------------------- ### Enhanced Entropy V1 (Before Migration) Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/releases.md Shows the old API for enhanced hashing before the namespace change and V2 split. Use this as a reference when migrating to V5. ```csharp var enhancedHashPassword = BCrypt.EnhancedHashPassword(myPassword); var validatePassword = BCrypt.EnhancedVerify(myPassword, enhancedHashPassword); ``` -------------------------------- ### Load raw asset with FileSystem.OpenAppPackageFileAsync Source: https://github.com/bcryptnet/bcrypt.net/blob/main/examples/AndroidTest/Resources/Raw/AboutAssets.txt Loads a raw asset (e.g., AboutAssets.txt) at runtime using the Essentials FileSystem API. The stream is read with a StreamReader to obtain the file contents. ```C# async Task LoadMauiAsset() { using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt"); using var reader = new StreamReader(stream); var contents = reader.ReadToEnd(); } ``` -------------------------------- ### MauiAsset Build Action in .csproj Source: https://github.com/bcryptnet/bcrypt.net/blob/main/examples/AndroidTest/Resources/Raw/AboutAssets.txt Declares raw assets in the Resources\Raw directory for deployment with the application package. The LogicalName metadata preserves the relative directory structure and file extension. ```XML ``` -------------------------------- ### Cost and Iterations Table Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/salts.md Table listing the number of iterations for each cost factor from 8 to 31. The minimum allowed cost is 4 for compatibility, and the maximum is 31. ```text | Cost | Iterations | |-------|--------------------------| | 8 | 256 iterations | | 9 | 512 iterations | | 10 | 1,024 iterations | | 11 | 2,048 iterations | | 12 | 4,096 iterations | | 13 | 8,192 iterations | | 14 | 16,384 iterations | | 15 | 32,768 iterations | | 16 | 65,536 iterations | | 17 | 131,072 iterations | | 18 | 262,144 iterations | | 19 | 524,288 iterations | | 20 | 1,048,576 iterations | | 21 | 2,097,152 iterations | | 22 | 4,194,304 iterations | | 23 | 8,388,608 iterations | | 24 | 16,777,216 iterations | | 25 | 33,554,432 iterations | | 26 | 67,108,864 iterations | | 27 | 134,217,728 iterations | | 28 | 268,435,456 iterations | | 29 | 536,870,912 iterations | | 30 | 1,073,741,824 iterations | | 31 | 2,147,483,648 iterations | etc ``` -------------------------------- ### Enhanced Entropy V2 - Hash and Verify with Default SHA384 Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/enhancedEntropy.md Use BCryptExtendedV2 to hash a password with enhanced entropy and verify it. The default SHA384 hashing is applied before bcrypt. The HMAC key is not stored in the hash, so you must keep it available for verification. ```csharp var enhancedHashPassword = BCryptExtendedV2.HashPassword(myPassword); var validatePassword = BCryptExtendedV2.Verify(myPassword, enhancedHashPassword); ``` -------------------------------- ### Enhanced Entropy V2 - Explicitly Set SHA384 Hash Type Source: https://github.com/bcryptnet/bcrypt.net/blob/main/docs/docs/enhancedEntropy.md Use BCryptExtendedV2 with an explicit hashType parameter to set the SHA version used for pre-hashing. This is useful for compatibility with libraries like PassLib (SHA256) or DropBox (SHA512), or to guard against future changes in the library's default. ```csharp var enhancedHashPassword = BCryptExtendedV2.HashPassword(myPassword, hashType: HashType.SHA384); var validatePassword = BCryptExtendedV2.Verify(myPassword, enhancedHashPassword, hashType:HashType.SHA384); ```