# crypto

***

**1. Encrypting and Decrypting Data with AES-256**

```javascript
const crypto = require('crypto');

// Encrypting data
const encryptedData = crypto.createCipher('aes-256-cbc', 'myPassword').update('Hello World').final('hex');

// Decrypting data
const decryptedData = crypto.createDecipher('aes-256-cbc', 'myPassword').update(encryptedData, 'hex').final('utf8');
```

**2. Hashing Data with SHA-256**

```javascript
const crypto = require('crypto');

const hash = crypto.createHash('sha256').update('Hello World').digest('hex');
```

**3. Signing Data with RSA**

```javascript
const crypto = require('crypto');

const privateKey = crypto.createPrivateKey({
  key: fs.readFileSync('private.pem'),
  format: 'pem',
});

const signature = crypto.sign('sha256', Buffer.from('Hello World'), privateKey);
```

**4. Verifying Signature with RSA**

```javascript
const crypto = require('crypto');

const publicKey = crypto.createPublicKey({
  key: fs.readFileSync('public.pem'),
  format: 'pem',
});

const verified = crypto.verify('sha256', Buffer.from('Hello World'), publicKey, signature);
```

**5. Generating a Random String**

```javascript
const crypto = require('crypto');

const randomString = crypto.randomBytes(32).toString('hex');
```

**6. Generating a Secure Random Number**

```javascript
const crypto = require('crypto');

const randomNumber = crypto.randomInt(100);
```

**7. Encrypting and Decrypting Data with HMAC**

```javascript
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'mySecret');

// Encrypting data
const encryptedData = hmac.update('Hello World').digest('hex');

// Decrypting data
const decryptedData = hmac.update('Hello World').digest('hex');
```

**8. Encrypting and Decrypting Data with Cipher IV**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipheriv('aes-256-cbc', 'myPassword', Buffer.from('00000000000000000000000000000000'));

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');

// Decrypting data
const decipher = crypto.createDecipheriv('aes-256-cbc', 'myPassword', Buffer.from('00000000000000000000000000000000'));
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**9. Encrypting and Decrypting Data with GCM**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipheriv('aes-256-gcm', 'myPassword', Buffer.from('00000000000000000000000000000000'));

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
const authTag = cipher.getAuthTag();

// Decrypting data
const decipher = crypto.createDecipheriv('aes-256-gcm', 'myPassword', Buffer.from('00000000000000000000000000000000'));
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
const authTagVerified = decipher.setAuthTag(authTag).verify();
```

**10. Generating a Diffie-Hellman Key Pair**

```javascript
const crypto = require('crypto');
const dh = crypto.createDiffieHellman(512);

// Generating private key
const privateKey = dh.generateKeys();

// Generating public key
const publicKey = dh.getPublicKey();
```

**11. Exchanging a Secret with Diffie-Hellman**

```javascript
const crypto = require('crypto');
const dh1 = crypto.createDiffieHellman(512);
const dh2 = crypto.createDiffieHellman(512);

// Generating private keys
const privateKey1 = dh1.generateKeys();
const privateKey2 = dh2.generateKeys();

// Generating public keys
const publicKey1 = dh1.getPublicKey();
const publicKey2 = dh2.getPublicKey();

// Exchanging secrets
const secret1 = dh1.computeSecret(publicKey2);
const secret2 = dh2.computeSecret(publicKey1);
```

**12. Generating an RSA Key Pair**

```javascript
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
});
```

**13. Encrypting Data with Public RSA Key**

```javascript
const crypto = require('crypto');
const publicKey = crypto.createPublicKey({
  key: fs.readFileSync('public.pem'),
  format: 'pem',
});

const encryptedData = crypto.publicEncrypt(publicKey, Buffer.from('Hello World'));
```

**14. Decrypting Data with Private RSA Key**

```javascript
const crypto = require('crypto');
const privateKey = crypto.createPrivateKey({
  key: fs.readFileSync('private.pem'),
  format: 'pem',
});

const decryptedData = crypto.privateDecrypt(privateKey, Buffer.from(encryptedData));
```

**15. Generating an ECDSA Key Pair**

```javascript
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('ec', {
  namedCurve: 'secp256k1',
});
```

**16. Signing Data with Private ECDSA Key**

```javascript
const crypto = require('crypto');
const privateKey = crypto.createPrivateKey({
  key: fs.readFileSync('private.pem'),
  format: 'pem',
});

const signature = crypto.sign('sha256', Buffer.from('Hello World'), privateKey);
```

**17. Verifying Signature with Public ECDSA Key**

```javascript
const crypto = require('crypto');
const publicKey = crypto.createPublicKey({
  key: fs.readFileSync('public.pem'),
  format: 'pem',
});

const verified = crypto.verify('sha256', Buffer.from('Hello World'), publicKey, signature);
```

**18. Generating a DSA Key Pair**

```javascript
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('dsa', {
  modulusLength: 1024,
});
```

**19. Signing Data with Private DSA Key**

```javascript
const crypto = require('crypto');
const privateKey = crypto.createPrivateKey({
  key: fs.readFileSync('private.pem'),
  format: 'pem',
});

const signature = crypto.sign('sha256', Buffer.from('Hello World'), privateKey);
```

**20. Verifying Signature with Public DSA Key**

```javascript
const crypto = require('crypto');
const publicKey = crypto.createPublicKey({
  key: fs.readFileSync('public.pem'),
  format: 'pem',
});

const verified = crypto.verify('sha256', Buffer.from('Hello World'), publicKey, signature);
```

**21. Generating a DH Key Pair**

```javascript
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('dh', {
  primeLength: 2048,
});
```

**22. Exchanging a Secret with DH**

```javascript
const crypto = require('crypto');
const dh1 = crypto.createDiffieHellman(2048);
const dh2 = crypto.createDiffieHellman(2048);

// Generating private keys
const privateKey1 = dh1.generateKeys();
const privateKey2 = dh2.generateKeys();

// Generating public keys
const publicKey1 = dh1.getPublicKey();
const publicKey2 = dh2.getPublicKey();

// Exchanging secrets
const secret1 = dh1.computeSecret(publicKey2);
const secret2 = dh2.computeSecret(publicKey1);
```

**23. Encrypting Data with AES in CBC Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('aes-256-cbc', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**24. Decrypting Data with AES in CBC Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes-256-cbc', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**25. Encrypting Data with AES in CFB Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('aes-256-cfb', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**26. Decrypting Data with AES in CFB Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes-256-cfb', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**27. Encrypting Data with AES in OFB Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('aes-256-ofb', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**28. Decrypting Data with AES in OFB Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes-256-ofb', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**29. Encrypting Data with AES in CTR Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('aes-256-ctr', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**30. Decrypting Data with AES in CTR Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes-256-ctr', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**31. Encrypting Data with Triple DES in ECB Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('des-ede3-ecb', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**32. Decrypting Data with Triple DES in ECB Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('des-ede3-ecb', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**33. Encrypting Data with Triple DES in CBC Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('des-ede3-cbc', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**34. Decrypting Data with Triple DES in CBC Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('des-ede3-cbc', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```

**35. Encrypting Data with Blowfish in CBC Mode**

```javascript
const crypto = require('crypto');
const cipher = crypto.createCipher('bf-cbc', 'myPassword');

// Encrypting data
const encryptedData = cipher.update('Hello World').final('hex');
```

**36. Decrypting Data with Blowfish in CBC Mode**

```javascript
const crypto = require('crypto');
const decipher = crypto.createDecipher('bf-cbc', 'myPassword');

// Decrypting data
const decryptedData = decipher.update(encryptedData, 'hex').final('utf8');
```
