Skip to content

Instantly share code, notes, and snippets.

@nkhil
nkhil / script.md
Created April 20, 2023 11:13
Parse a csv file in node without additional dependencies

Parse a CSV file in node without dependencies

We're going to parse a file, use a conditional to add a new column, then save the new file with an additional column all without using any external dependencies not included in the standard lib

const fs = require('fs');

const inputCsvFile = 'input.csv';
const outputCsvFile = 'output.csv';

Custom Karabiner rule to toggle W/A/S/D as arrow keys

The following rule re-maps the capslock key so it toggles on W/A/S/D becoming arrow keys.

{
  "global": {
    "check_for_updates_on_startup": true,
    "show_in_menu_bar": true,
    "show_profile_name_in_menu_bar": false,
@nkhil
nkhil / symmetric-decryption-nodejs.md
Created June 6, 2022 20:27
Symmetric decryption using nodejs
import never from 'never'
type ParsedLogs = Array<[string, string]>
type GroupedLogs = {
[K: string]: Array<string>
}
export default function main(logs: string) {
const parsedLogs = parseAndNormaliseLogs(logs)
function parseLogs(logs) {
const logsArray = logs.split('\n').map(eachLog => eachLog.split(','))
const normalisedArray = normaliseArray(logsArray)
const logsWithSeconds = normalisedArray.map(([timestamp, phoneNumber]) =>
[calculateTotalSeconds(timestamp), phoneNumber])
const grouped = groupByPhoneNumber(logsWithSeconds)
const phoneNumberWithLargestTotal = getPhoneNumberWithLargestTotalSeconds(grouped)
grouped[phoneNumberWithLargestTotal] = 0
return calculateTotalBill(logsWithSeconds, phoneNumberWithLargestTotal)
}
const crypto = require('crypto')
const fs = require('fs')
const encryptedData = fs.readFileSync('encrypted_data.txt', { encoding: 'utf-8' })
const privateKey = fs.readFileSync('private.pem', { encoding: 'utf-8' })
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
// In order to decrypt the data, we need to specify the
const fs = require('fs')
const crypto = require('crypto')
const dataToEncrypt = fs.readFileSync('data_to_encrypt.txt', { encoding: 'utf-8' })
const publicKey = Buffer.from(fs.readFileSync('public.pem', { encoding: 'utf-8' }))
const encryptedData = crypto.publicEncrypt(
{
key: publicKey,
const crypto = require('crypto')
const fs = require('fs')
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
// *********************************************************************
//
const crypto = require('crypto')
const NUMBER_OF_BYTES = 32
const randomBytes = crypto.randomBytes(NUMBER_OF_BYTES)

Here's how to create and export an RSA public/private key pair from Node.js

const crypto = require('crypto')
const fs = require('fs')

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