sha2 / bcrypt for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

sha2 / bcrypt for Rust: Complete Guide with Examples

\n\n

In modern development, data security is a critical aspect of any application. The sha2 and bcrypt libraries for the Rust language provide powerful and secure tools for cryptographic hashing. sha2 implements the SHA-2 (Secure Hash Algorithm 2) family of algorithms, including SHA-224, SHA-256, SHA-384, SHA-512, as well as their truncated variants (SHA-512/224, SHA-512/256). bcrypt is an adaptive cryptographic hash function specifically designed for secure password storage. It uses a salt and a configurable number of rounds (cost factor), making it resistant to rainbow table attacks and brute force.

\n\n

Why is this needed? SHA-2 is used for data integrity verification, creating digital signatures, in version control systems (Git), blockchain, and many other areas where a fast and reliable hash is required. Bcrypt, in turn, is the de facto standard for password hashing in web applications, databases, and authentication systems. Its key advantage is configurable "slowness": you can increase the number of rounds to compensate for the growth of an attacker's computing power.

\n\n

In this article, we will take a detailed look at both libraries: installation, main methods, real-world usage examples, and common mistakes. You will learn how to properly hash data, verify passwords, and integrate these functions into your Rust projects.

\n\n

Installation

\n\n

To use sha2 and bcrypt in Rust, you need to add them to your project's Cargo.toml file. Open a terminal in the project root directory and run:

\n\n
cargo add sha2 bcrypt
\n\n

This command will add the latest versions of the libraries to the [dependencies] section. Alternatively, you can manually edit Cargo.toml:

\n\n
[dependencies]\nsha2 = "0.10"\nbcrypt = "0.15"
\n\n

After that, run cargo build to download the dependencies and ensure everything is installed correctly.

\n\n

Quick Start: Minimal Working Example

\n\n

Let's start with a simple example: hashing a string with SHA-256 and verifying a password with bcrypt.

\n\n
use sha2::{Sha256, Digest};\nuse bcrypt::{hash, verify, DEFAULT_COST};\n\nfn main() {\n    // SHA-256 hashing\n    let data = b"Hello, world!";\n    let mut hasher = Sha256::new();\n    hasher.update(data);\n    let result = hasher.finalize();\n    println!("SHA-256 hash: {:x}", result);\n\n    // Bcrypt password hashing\n    let password = "my_secure_password";\n    let hashed = hash(password, DEFAULT_COST).unwrap();\n    println!("Bcrypt hash: {}", hashed);\n\n    // Password verification\n    let is_valid = verify(password, &hashed).unwrap();\n    println!("Password is valid: {}", is_valid);\n}
\n\n

This code demonstrates the basic capabilities of both libraries. The output will contain a 64-character hex SHA-256 hash and a long bcrypt string (starting with $2b$).

\n\n

Main Methods and Functions

\n\n

Let's look at the key functions and methods from sha2 and bcrypt. For each, we provide the signature, description, and a working example.

\n\n

1. sha2::Sha256::new()

\n

Signature: pub fn new() -> Sha256

\n

Description: Creates a new SHA-256 hasher instance. This is the starting point for hashing data.

\n
use sha2::{Sha256, Digest};\n\nlet mut hasher = Sha256::new();\nhasher.update(b"test data");\nlet hash = hasher.finalize();\nprintln!("{:x}", hash);
\n\n

2. sha2::Sha256::update()

\n

Signature: pub fn update(&mut self, dat

Recommendations