Skip to content

Instantly share code, notes, and snippets.

View earlpeterg's full-sized avatar

Earl Peter G earlpeterg

View GitHub Profile
@earlpeterg
earlpeterg / .htaccess
Last active December 23, 2022 02:13
ReactJS/AngularJS/VueJS Optimized Htaccess
## Remove next lines if you don't have HTTPS
<IfModule mod_headers.c>
Header always set Content-Security-Policy: upgrade-insecure-requests
</IfModule>
# BEGIN Cache-Control/Deflate/GZIP Compression
<IfModule pagespeed_module>
ModPagespeed on
</IfModule>
@earlpeterg
earlpeterg / INIHelper.cs
Last active March 19, 2022 06:34
A helper class to manipulate INI configuration files (*.ini, *.cfg, *.conf, or even *.txt)
/// <summary>
/// A helper class to manipulate INI configuration files (*.ini, *.cfg, *.conf, or even *.txt)
///
/// Usage of values:
/// (Name of setting)=(string value)
///
/// Sections (optional) may be used to group values under it
/// Section names are in between brackets [ and ].
///
/// References:
@earlpeterg
earlpeterg / BicubicInterpolation.cs
Last active October 27, 2022 10:41
Scaling Algorithms in C#
using System;
using System.Drawing;
/// <summary>
/// Performs a bicubic interpolation over the given matrix to produce a
/// [<paramref name="newHeight"/>, <paramref name="newWidth"/>] matrix.
/// </summary>
/// <remarks>
/// Note, dimensions of the input and output matrices are in
/// conventional matrix order, like [matrix_height, matrix_width],
@earlpeterg
earlpeterg / minimax-tictactoe.py
Created May 25, 2021 07:31
Python Minimax AI of TicTacToe based on MatthewSteel's Implementation
# Minimax AI of TicTacToe based on MatthewSteel's
# C Implementation: https://gist.github.com/MatthewSteel/3158579
def gridChar(i):
if (i == -1): return 'X'
if (i == 1): return 'O'
return ' '
def draw(board):
@earlpeterg
earlpeterg / duplicateImageCleaner.py
Created April 28, 2021 08:21
Duplicate Image Cleaner in Python (Find and delete duplicate images from a directory)
from PIL import Image # pip install Pillow
from send2trash import send2trash # pip install Send2Trash
import glob
import imagehash # pip install imagehash
import os, os.path
import sys
rootDir = input("Directory: ")
cutoff = 3 # 1 to 5, higher value lesser sensitive
@earlpeterg
earlpeterg / SmtpHelper.cs
Last active March 31, 2021 05:15
C# SMTP Helper
public class SmtpHelper {
private const int Timeout = 180000;
public string Host { get; set; }
public int Port { get; set; } = 587;
public bool IsSecured { get; set; } = true;
public string Sender { get; set; }
public string SenderDisplayName { get; set; }
public string Password { get; set; }
public List<MailAddress> Recipients { get; set; } = new List<MailAddress> { };
@earlpeterg
earlpeterg / otp.cs
Last active March 31, 2021 03:26
C# Time-based One-time Password (TOTP using HOTP key based on a timestamp and window size)
/// <summary>
/// Time-based One-time Password (TOTP using HOTP key based on a timestamp and window size)
/// Note: needs reference to Base32 from https://stackoverflow.com/a/641437/4469947
///
/// Usage:
/// Otp::generateSecret(); <- this will be your user's secret key (save it to your db)
/// Optionally, you can display the QR code for user's authenticator app using Otp::generateQr(...)
///
/// To verify an OTP code provided during login, use VerifyPasscode(...)
///
@earlpeterg
earlpeterg / timebased_otp.php
Created March 28, 2021 14:48
Time-based One-time Password (TOTP) using HOTP key based on a timestamp and window size
<?php
/**
* Time-based One-time Password (TOTP using HOTP key based on a timestamp and window size)
*
* Usage:
* 1) Otp::generateSecret(); <- this will be your user's secret key (save it to your db)
* Optionally, you can display the QR code for user's authenticator app using Otp::generateQr(...)
* 2) You can call Otp::generatePasscode(...) === $userInputOtp to check if provided OTP is valid.
*
* If you need a simple random 6-digit OTP, you can call Otp::generateSimple().
@earlpeterg
earlpeterg / PasswordDecipher.cs
Last active March 31, 2020 07:29
C# Password Decipher, Generates Password With Salt
public static class PasswordDecipher
{
// Example: 3d3c1fd887fec9d1f9a3ecb0bad1f070d677e2cff6ef632acb417be4a8bbea1e:23ae04889a7d1ef9 is qwerty1234
/// <summary>
/// Checks if a plain password matches the hashed password with salting
/// </summary>
/// <param name="hashedpassword">SHA256-Hashed password + random salt</param>
public static bool IsValid(string hashedpassword, string plainpassword)
{
@earlpeterg
earlpeterg / HttpHelper.cs
Last active March 19, 2022 06:17
C# HTTP Helper Functions (HTTP GET, POST)
/// <summary>
/// Requires the following packages:
/// Newtonsoft.Json
/// System.ValueTuple
/// Also requires HttpQueryStringBuilder class.
/// Example use:
/// HttpHelper.Get("https://api.example.com/status", new { userId = "1234" });
/// will result to https://api.example.com/status?userId=1234
/// </summary>