Cryptography in TypeScript: Data Encryption and Hashing
In modern web development, data security is a critically important aspect. TypeScript, as a strictly typed superset of JavaScript, provides powerful tools for implementing cryptographic operations. In this article, we will look at the main encryption and hashing algorithms available in TypeScript and show how to apply them in practice.
Built-in crypto module and libraries
Node.js provides a built-in crypto module that is fully compatible with TypeScript. For browser applications, the Web Crypto API is used. For more complex tasks, the crypto-js library is recommended, which works both in Node.js and in the browser.
Installing the necessary packages:
npm install crypto-jsnpm install @types/crypto-js --save-devSymmetric AES Encryption
The AES (Advanced Encryption Standard) algorithm is the most popular symmetric cipher. It uses the same key for encrypting and decrypting data.
Example of AES-256 implementation in TypeScript:
import CryptoJS from 'crypto-js';
const SECRET_KEY = 'my-strong-secret-key-32chars';
function encryptAES(text: string): string { const encrypted = CryptoJS.AES.encrypt(text, SECRET_KEY); return encrypted.toString();}
function decryptAES(ciphertext: string): string { const decrypted = CryptoJS.AES.decrypt(ciphertext, SECRET_KEY); return decrypted.toString(CryptoJS.enc.Utf8);}
// Usageconst originalText = 'Secret data for protection';const encrypted = encryptAES(originalText);console.log('Encrypted:', encrypted);
const decrypted = decryptAES(encrypted);console.log('Decrypted:', decrypted);Asymmetric RSA Encryption
RSA uses a pair of keys: a public key and a private key. Data is encrypted with the public key and decrypted with the private key. This is ideal for secure data transmission.
Example using Node.js crypto:
import * as crypto from 'crypto';
function generateRSAKeys(): { publicKey: string; privateKey: string } { const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' } }); return { publicKey, privateKey };}
function encryptRSA(plainText: string, publicKey: string): string { const buffer = Buffer.from(plainText, 'utf-8'); const encrypted = crypto.publicEncrypt(publicKey, buffer); return encrypted.toString('base64');}
function decryptRSA(cipherText: string, privateKey: string): string { const buffer = Buffer.from(cipherText, 'base64'); const decrypted = crypto.privateDecrypt(privateKey, buffer); return decrypted.toString('utf-8');}
// Usage exampleconst keys = generateRSAKeys();const message = 'Confidential message';const encryptedRSA = encryptRSA(message, keys.publicKey);const decryptedRSA = decryptRSA(encryptedRSA, keys.privateKey);
console.log('Original:', message);console.log('Decrypted:', decryptedRSA);Data Hashing
Hashing is a one-way transformation of data into a fixed-length string. Unlike encryption, a hash cannot be reversed. Main applications:
- Password storage — storing the hash instead of the password
- Data integrity verification — comparing file hashes
- Digital signatures — confirming authorship
SHA-256
The most common algorithm of the SHA-2 family:
import CryptoJS from 'crypto-js';
function hashSHA256(data: string): string { return CryptoJS.SHA256(data).toString();}
Also in library
Working with Dates in Swift (iOS/macOS): Libraries and Frameworks
NetworkExtension for Swift: Complete Guide with Examples
Polars for Rust: Complete Guide with Examples
Object Serialization in Swift (iOS/macOS): Codable, JSON and PropertyList
Event Bus and Observer Pattern in Go (Golang): Implementation and Examples