Secure Coding & OWASP Top 10 for Backend · บทเรียน

การจัดการกุญแจและการแฮช

สำรวจแนวทางปฏิบัติที่ปลอดภัยในการจัดการกุญแจเข้ารหัส ใช้อัลกอริทึมแฮชที่แข็งแกร่งกับรหัสผ่าน และหลีกเลี่ยงข้อผิดพลาดด้านการเข้ารหัสที่พบบ่อย

บทเรียน 3 จาก 412 ขั้นตอน

การจัดการกุญแจและการแฮช เป็นบทเรียน Secure Coding & OWASP Top 10 for Backend ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Secure Coding & OWASP Top 10 for Backend และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Cryptographic Keys

Welcome to this lesson on Key Management and Hashing! We'll explore how to protect the secrets that protect your data.

Cryptographic keys are fundamental to secure communication and data storage. Think of them as secret passwords or unique stamps that lock and unlock sensitive information.

Why Key Security Matters

The security of your entire system often depends on the security of your cryptographic keys.

  • Data Breaches: If an attacker gains access to your encryption keys, all data encrypted with those keys becomes readable.
  • Impersonation: Compromised signing keys can allow attackers to forge identities or tamper with data without detection.
  • Trust Erosion: Loss of keys can lead to a complete breakdown of trust in your system's security posture.

Generating Strong Keys

Keys must be truly random and sufficiently long to be secure. Weak or predictable keys are easy for attackers to guess.

Always use cryptographically secure random number generators (CSRNGs) provided by your programming language's standard library. Never roll your own!

Try running this example to see how a secure key can be generated:

import java.security.SecureRandom;
import java.util.Base64;

public class KeyGenerator {
  public static void main(String[] args) {
    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[32]; // 256-bit key
    random.nextBytes(keyBytes);
    String base64Key = Base64.getEncoder().encodeToString(keyBytes);
    System.out.println("Generated Key: " + base64Key);
  }
}

Secure Key Storage

Once generated, keys need to be stored securely. This is one of the most critical aspects of key management.

  • Hardware Security Modules (HSMs): Dedicated physical devices for secure key generation, storage, and cryptographic operations.
  • Key Management Services (KMS): Cloud-based services (e.g., AWS KMS, Azure Key Vault) that provide secure key storage and lifecycle management.
  • Avoid: Storing keys directly in source code, configuration files, or version control.

Key Rotation for Longevity

Even with the best storage, keys can eventually be compromised. Regular key rotation limits the damage if a key is ever exposed.

Key rotation involves generating a new key, re-encrypting data with the new key, and securely archiving or destroying the old key. This reduces the 'window of exposure' for any single key.

Understanding Hashing

Hashing is a one-way process that transforms input data into a fixed-size string of characters, called a hash or digest.

  • One-way: You can't easily reverse a hash to get the original data.
  • Fixed-size: No matter the input size, the output hash is always the same length.
  • Unique (mostly): A tiny change in input results in a vastly different hash.

Hashing is crucial for verifying data integrity and securely storing passwords.

Hashing Passwords Securely

Never store user passwords in plain text or encrypted form. Always store their hash.

If a database is breached, attackers only get hashes, not the actual passwords. Since hashing is one-way, they can't easily recover the original passwords.

However, simple hashing isn't enough on its own. We need more techniques!

The Power of Salting

A salt is a unique, random string added to a password before it's hashed. Each user gets a different salt.

Salting prevents rainbow table attacks, where attackers pre-compute hashes for common passwords. With salts, even if two users have the same password, their stored hashes will be completely different.

This example conceptually shows how a salt is added before hashing:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class PasswordHasher {
  public static void main(String[] args) throws NoSuchAlgorithmException {
    String password = "mySecretPassword";
    
    // Generate a random salt for each user
    SecureRandom random = new SecureRandom();
    byte[] saltBytes = new byte[16]; // 128-bit salt
    random.nextBytes(saltBytes);
    String salt = Base64.getEncoder().encodeToString(saltBytes);
    
    // Combine password and salt, then hash
    String saltedPassword = password + salt;
    MessageDigest md = MessageDigest.getInstance("SHA-256"); // Illustrative
    byte[] hashedPasswordBytes = md.digest(saltedPassword.getBytes());
    String hashedPassword = Base64.getEncoder().encodeToString(hashedPasswordBytes);
    
    System.out.println("Password: " + password);
    System.out.println("Salt: " + salt);
    System.out.println("Hashed Password (with salt): " + hashedPassword);
  }
}

Modern Hashing Algorithms

For password hashing, don't use general-purpose hash functions like SHA-256 or MD5. They are too fast, making brute-force attacks easier.

Instead, use algorithms specifically designed to be slow and computationally intensive:

  • Bcrypt: Widely used and highly recommended.
  • Scrypt: Another strong option, especially resistant to GPU-based attacks.
  • Argon2: The winner of the Password Hashing Competition, considered state-of-the-art.

These algorithms have adjustable 'work factors' to increase their computational cost over time.

Avoiding Crypto Pitfalls

Cryptography is complex. Common mistakes can severely weaken your security:

  • Don't 'Roll Your Own' Crypto: Always use well-vetted, standard cryptographic libraries. Custom implementations are almost always insecure.
  • Hardcoding Keys: Never embed encryption keys directly in your code.
  • Using Weak Algorithms: Avoid deprecated or known-vulnerable algorithms (e.g., MD5, SHA1 for security, DES, RC4).
  • Improper Randomness: Don't use non-cryptographically secure random number generators for security tasks.

Test Your Knowledge

Which of the following are recommended best practices for managing cryptographic keys and passwords?

Recap & Next Steps

In this lesson, we've covered the vital aspects of cryptographic key management and secure password hashing.

  • Keys: Generate strong, random keys, store them securely (HSM/KMS), and rotate them regularly.
  • Hashing: Always hash passwords using unique salts and slow, purpose-built algorithms like bcrypt, scrypt, or Argon2.
  • Avoid Pitfalls: Never create your own crypto, hardcode keys, or use weak algorithms.

By following these practices, you significantly strengthen your backend applications against data breaches and unauthorized access. Keep learning and stay secure!

เริ่มต้นได้ฟรี

เรียนรู้ Secure Coding & OWASP Top 10 for Backend ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจัดการกุญแจและการแฮช” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการกุญแจและการแฮช” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Secure Coding & OWASP Top 10 for Backend ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการกุญแจและการแฮช”

สำรวจแนวทางปฏิบัติที่ปลอดภัยในการจัดการกุญแจเข้ารหัส ใช้อัลกอริทึมแฮชที่แข็งแกร่งกับรหัสผ่าน และหลีกเลี่ยงข้อผิดพลาดด้านการเข้ารหัสที่พบบ่อย คุณปฏิบัติ Secure Coding & OWASP Top 10 for Backend ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Secure Coding & OWASP Top 10 for Backend หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Secure Coding & OWASP Top 10 for Backend บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการกุญแจและการแฮช” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Secure Coding & OWASP Top 10 for Backend นี้ได้ไหม

ได้ บทเรียน Secure Coding & OWASP Top 10 for Backend ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การปกป้องข้อมูลละเอียดอ่อนขณะจัดเก็บ
  2. การรักษาความปลอดภัยข้อมูลระหว่างส่ง (TLS/SSL)
  3. การจัดการกุญแจและการแฮช
  4. การจัดการข้อมูลลับอย่างปลอดภัย
← กลับไปที่ Secure Coding & OWASP Top 10 for Backend