Skip to content

Instantly share code, notes, and snippets.

View mudssrali's full-sized avatar
🕸️
Crafting software solutions for better, at scale

Mudassar mudssrali

🕸️
Crafting software solutions for better, at scale
View GitHub Profile
@mudssrali
mudssrali / erlang-elixir-aes-crypto.ex
Last active September 15, 2021 20:56
Elixir implementation of encryption and decryption using erlang's :crypto.block_encrypt and :crypto.block_decrypt
defmodule Crypto.AES do
@block_size 16
@secret_key ""
def encrypt(text) do
secret_key_hash = make_hash(@secret_key, 16)
text = pad_pkcs7(text, @block_size)
encrypted_text = :crypto.block_encrypt(:aes_ecb, secret_key_hash, text)
Base.encode64(encrypted_text)
@mudssrali
mudssrali / aes-nodejs-service.js
Created September 14, 2021 04:09
a simple AES service to encrypt and decrypt plaintext
const express = require("express");
const cors = require('cors');
const crypto = require('crypto-js');
const PORT = process.env.PORT ?? 8080;
const HOST = process.env.HOST ?? '0.0.0.0';
const SECRET_KEY = process.env.SECRET_KEY ?? "write something secret here";
const app = express();
app.use(cors());
@mudssrali
mudssrali / erlang-elixir-crypto_one_time.ex
Last active September 7, 2021 01:59
Implementation of erlang's :crypto.crypto_one_time/5 API with base64 encoding in elixir
defmodule AES do
@block_size 16
@secret_key "write something secret"
def encrypt(text) do
secret_key_hash = hash(@secret_key, 32)
# create random Initialisation Vector
iv = :crypto.strong_rand_bytes(16)
text = pad(text, @block_size)
@mudssrali
mudssrali / random-string-generator.js
Created June 19, 2021 11:30
Generate a random string of variable length
const generateRandomString = (stringLength) => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
let randomString = ''
for (let i = 0; i < Math.abs(stringLength); i++) {
randomString += characters.charAt(Math.floor(Math.random() * charactersLength))
}
return randomString

Keybase proof

I hereby claim:

  • I am mudssrali on github.
  • I am mudssrali (https://keybase.io/mudssrali) on keybase.
  • I have a public key whose fingerprint is CD9B 8112 82E3 12AA 2390 367F A1D9 A7D9 3FEF 2B1B

To claim this, I am signing this object:

React custom hook to show/hide content based on outside click

import React, { useState, useEffect, useRef } from 'react'

export const useComponentVisible = (isVisible = false) => {
  	// ref of elem for which you want to detect outside click
	const ref = useRef(null)
	const [isComponentVisible, setIsComponentVisible] = useState(isVisible)
@mudssrali
mudssrali / useStripe.md
Created March 20, 2021 06:53
Load stripe.js dynamically in Reactjs using hook - useStripe
export const useStripe = () => {
	const [stripe, setStripe] = useState()

	useEffect(() => {
		const stripeJs = document.createElement('script')
		stripeJs.src = 'https://js.stripe.com/v3/'
		stripeJs.async = true
		stripeJs.onload = () => {
 setStripe(window.Stripe(STRIPE_API_KEY))
@mudssrali
mudssrali / setup-cra-typescript.md
Last active March 13, 2021 18:13
a helper markdown to setup new CRA app with typescript template and tailwindcss

Setup CRA app with typescript template and tailwind

I always avoid DRY (don't repeat yourself), but for some reasons, I do this to refresh my mind, my knowledge about the toolings that I'm going to use in new project.

Time to follow steps:

  • Step 1

    npx create-react-app my-app --template typescript OR

@mudssrali
mudssrali / dev-machine-setup.md
Last active June 9, 2021 04:16
This gist contains all stuff to setup my dev machine and get-started for cerp-labs and personal usage

Following things are required must be installed on ubuntu (Debian) [I use ubuntu linux]:

  • Elixir

    • Add Erlang Solutions repository: wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
    • Run: sudo apt-get update
    • Install the Erlang/OTP platform and all of its applications: sudo apt-get install esl-erlang
    • Install Elixir: sudo apt-get install elixir
  • Docker

@mudssrali
mudssrali / check_compulsory_fields.md
Last active March 23, 2021 06:38
check compulsory fields in an object
// @param obj: for which the given properties exist
// @param fields[]: is an array of given object properties
// @return false if every field is not blank
// @return an array of labels that are blank

const checkCompulsoryFields = (obj: any, fields: string[]): boolean | string[] => {
	const list = fields.filter(field => field in obj && obj[field].trim() === "")
	return list.length === 0 ? false : list