مقدمة عن تشفير البيانات
تشفير البيانات يحمي المعلومات الحساسة من الوصول غير المصرح به. ينقسم إلى نوعين رئيسيين:
- تشفير أثناء النقل (In-Transit) - HTTPS/TLS
- تشفير أثناء التخزين (At-Rest) - تشفير قواعد البيانات والملفات
HTTPS و TLS
HTTPS هو الطبقة الأساسية لأمان API. يشفر كل البيانات بين العميل والخادم.
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
};
https.createServer(options, app).listen(443);
⚠️ استخدم خدمات مثل Let's Encrypt للحصول على شهادات SSL مجانية.
التشفير المتماثل (Symmetric Encryption)
نفس المفتاح يستخدم للتشفير وفك التشفير. سريع لكن يتطلب مشاركة المفتاح بأمان.
const crypto = require('crypto');
const algorithm = 'aes-256-gcm';
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
function encrypt(text) {
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag().toString('hex');
return { encrypted, iv: iv.toString('hex'), authTag };
}
function decrypt(encrypted, ivHex, authTagHex) {
const decipher = crypto.createCipheriv(
algorithm,
key,
Buffer.from(ivHex, 'hex')
);
decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
التشفير غير المتماثل (Asymmetric Encryption)
مفتاح عام للتشفير ومفتاح خاص لفك التشفير. أأمن لكن أبطأ.
const { generateKeyPairSync, publicEncrypt, privateDecrypt } = require('crypto');
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
});
const encrypted = publicEncrypt(publicKey, Buffer.from('secret data'));
const decrypted = privateDecrypt(privateKey, encrypted);
التشفير أثناء التخزين (At-Rest Encryption)
تشفير أعمدة قاعدة البيانات
const encryptedEmail = encrypt(userEmail);
await db.query(
'INSERT INTO users (email_encrypted, email_iv, email_auth) VALUES (?, ?, ?)',
[encryptedEmail.encrypted, encryptedEmail.iv, encryptedEmail.authTag]
);
تشفير الملفات
const fs = require('fs');
const zlib = require('zlib');
const crypto = require('crypto');
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const readStream = fs.createReadStream('file.txt');
const writeStream = fs.createWriteStream('file.enc');
readStream
.pipe(zlib.createGzip())
.pipe(cipher)
.pipe(writeStream);
التجزئة مقابل التشفير (Hashing vs Encryption)
- التجزئة (Hashing) - اتجاه واحد، يستخدم لكلمات المرور
- التشفير (Encryption) - اتجاهين، يستخدم للبيانات القابلة للاسترجاع
const bcrypt = require('bcrypt');
// تجزئة كلمة المرور (لا يمكن فكها)
const hash = await bcrypt.hash(password, 12);
// التحقق
const match = await bcrypt.compare(password, hash);
Node.js Crypto Module
const crypto = require('crypto');
// توليد مفاتيح عشوائية
const secret = crypto.randomBytes(64).toString('hex');
// HMac للتوقيع
const hmac = crypto.createHmac('sha256', secret);
hmac.update('message');
const signature = hmac.digest('hex');
أفضل الممارسات
- استخدم TLS 1.3 على الأقل
- لا تستخدم خوارزميات قديمة (DES, MD5, SHA-1)
- استخدم AES-256-GCM للتشفير الحديث
- خزن مفاتيح التشفير في vault منفصل عن البيانات
- قم بتدوير المفاتيح دورياً
ملخص
https شفير أثناء النقل، AES للتشفير المتماثل، RSA للتشفير غير المتماثل، bcrypt لكلمات المرور. كل منها له استخدامه الخاص في API.
🎯 التالي: أمان قاعدة البيانات