Skip to content

Instantly share code, notes, and snippets.

View rameshbaskar's full-sized avatar

Ramesh Baskarasubramanian rameshbaskar

View GitHub Profile
@rameshbaskar
rameshbaskar / JavaCryptoTest.java
Created March 18, 2020 06:00
Java - Cryptography Example
import static org.springframework.security.crypto.bcrypt.BCrypt.hashpw;
import static org.springframework.security.crypto.bcrypt.BCrypt.gensalt;
import static org.springframework.security.crypto.bcrypt.BCrypt.checkpw;
public class CryptoTest {
public static void main(String[] args) {
String plainText = "Password123";
String hash = hashpw(plainText, gensalt(12));
if (checkpw(plainText, hash)) {
System.out.println("Hashing verified.");
@rameshbaskar
rameshbaskar / jsPromiseSample.js
Last active September 10, 2020 08:25
Example JS code on Promises using async/await with promise rejection
async function capitalize(text, timeout) {
return new Promise((resolve, reject) => {
setTimeout(() => {
let firstChar = text.charAt(0);
if (text.charAt(0).toUpperCase() === firstChar) {
reject('First character is already capitalized');
} else {
resolve(text.charAt(0).toUpperCase() + text.slice(1));
}
}, timeout);
@rameshbaskar
rameshbaskar / nodeWait.js
Last active October 27, 2020 07:46
Waiting in NodeJS
// Using util.promisify()
async function sleep(ms) {
console.log('Waiting...');
await require('util').promisify(setTimeout)(ms);
}
console.log('initial log');
sleep(5000).then(() => {
console.log('after sleep');
});
@rameshbaskar
rameshbaskar / queryMysqlDatabase.js
Last active October 24, 2021 13:45
NodeJS query MySql database
import mysql from 'mysql';
const {
DB_HOST,
DB_USER,
DB_PASSWORD,
DB_NAME
} = process.env
export const database = mysql.createPool({
@rameshbaskar
rameshbaskar / queryCouchDB.js
Created January 18, 2021 02:11
NodeJS query CouchDB
import Nano from 'nano';
const {
DB_HOST,
DB_PORT,
DB_USER,
DB_PASSWORD,
DB_NAME
} = process.env
// a refValue of 0.5 will generate equal amount of true and false
// a refValue greater than 0.5 will generate more true values
// a refValue less than 0.5 will generate more false values
const randomBoolean = (positiveBias = false) => {
const refValue = positiveBias ? 0.8 : 0.5;
return Math.random() < refValue;
};
@rameshbaskar
rameshbaskar / read_json.py
Created October 24, 2021 13:38
Python - Reading data from a JSON file
import json
# No need to specify the "r" (ie. open the file in read-only mode) as it is the default
def parse_json(json_file_path):
json_data = None
try:
file = open(json_file_path, "r")
json_data = json.load(file)
except:
print("Something went wrong while opening JSON file: " + json_file_path)
@rameshbaskar
rameshbaskar / variable_reference_sample.py
Created October 24, 2021 14:26
Python - variable referencing and copy example
import copy
# Base list
list1 = ["apple", "banana", "cherry"]
print("By default Python creates variables by reference")
list2 = list1
print("Now while list2 and list1 seems like two variables they refer to the same memory reference")
print("So modifying one variable will modify the others")
@rameshbaskar
rameshbaskar / password_hashing.py
Created October 25, 2021 14:01
Python - Password encryption and verification
# ensure to run 'pip install passlib'
from passlib.context import CryptContext
plain_text = 'Password@123'
pwd_context = CryptContext(
schemes=['pbkdf2_sha256'],
default='pbkdf2_sha256',
pbkdf2_sha256__default_rounds=40000
)
@rameshbaskar
rameshbaskar / simple_array_sort.js
Created January 31, 2023 08:37
Simple array sorting in Javascript
function sortArray(arr) {
return arr.sort((a, b) => a - b);
}
const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(sortArray(arr));