Skip to content

Instantly share code, notes, and snippets.

View curse-github's full-sized avatar

Curse curse-github

View GitHub Profile
class Colors {
static Reset :string = "\x1b[0m";
static Bright :string = "\x1b[1m";
static Underscore:string = "\x1b[4m";
static Reverse :string = "\x1b[7m";
//static Dim :string = "\x1b[2m";//does not work at all
//static Blink :string = "\x1b[5m";//does not work at all
//static Hidden :string = "\x1b[8m";//does not work at all
static R :string = "\x1b[0m";
static B :string = "\x1b[1m";
import { hmac256 } from "./HMAC";
// implementation of HMAC-Based Key Derivation Function(HKDF)
// by Curse
// #region HKDF
// https://en.wikipedia.org/wiki/HKDF
// ikm = input key material
function hkdf_extract(salt, ikm) {
if (salt.length == 0) {
let newSalt = [];
for (let i = 0; i < 64; i++)
import { base32ToBytes, wordsToBytes } from "./numberHandling";
import { hmac } from "./HMAC";
// implementation of HMAC-Based One Time Password(HOTP)
// and Time Based One Time Password(TOTP)
// by Curse
const oneTimeDuration = 30 * 1000; // 30 seconds
const totpWindow = 1;
const hotpCodeLength = 6;
// #region TOTP
// https://en.wikipedia.org/wiki/Time-based_one-time_password
import { stringToBytes } from "./numberHandling";
import { sha1Bytes } from "./sha1";
import { sha256Bytes } from "./sha256";
// implementation of Hash-Based Message Authentication Code(HMAC) in javascript
// by Curse
// https://en.wikipedia.org/wiki/HMAC
export function hmac(key, message) {
if (typeof key == "string")
key = stringToBytes(key);
if (typeof message == "string")
import { stringToBytes, bytesToBase16, bytesToWords, wordsToBytes } from "./numberHandling";
// adapted from algorithm I found here https://stackoverflow.com/questions/59777670/how-can-i-hash-a-string-with-sha256, which was taken from https://geraintluff.github.io/sha256/
// and from pseudo-code at https://en.wikipedia.org/wiki/SHA-2
// by Curse
export function sha256(str) {
return bytesToBase16(sha256Bytes(stringToBytes(str)));
}
let h = [];
let k = [];
const maxWord = Math.pow(2, 32);
import { stringToBytes, bytesToBase16, bytesToWords, wordsToBytes } from "./numberHandling";
// adapted partially from algorithm I found here https://stackoverflow.com/questions/59777670/how-can-i-hash-a-string-with-sha256, which was taken from https://geraintluff.github.io/sha256/
// and the wikipedia article on SHA-1 https://en.wikipedia.org/wiki/SHA-1
// by Curse
export function sha1(str) {
return bytesToBase16(sha1Bytes(stringToBytes(str)));
}
// actual hash algorithms
function leftRotate(value, amount) {
return (value >>> (32 - amount)) | (value << amount);
import { stringToBytes, bytesToBase16, bytesToWords, wordsToBytes } from "./numberHandling";
// adapted partially from algorithm I found here https://stackoverflow.com/questions/59777670/how-can-i-hash-a-string-with-sha256, which was taken from https://geraintluff.github.io/sha256/
// and the wikipedia article on SHA-1 https://en.wikipedia.org/wiki/SHA-1
// by Curse
export function sha0(str) {
return bytesToBase16(sha0Bytes(stringToBytes(str)));
}
// actual hash algorithms
function leftRotate(value, amount) {
return (value >>> (32 - amount)) | (value << amount);
// general library for handling, converting, and generating numbers in other bases
// by Curse
function padStartToMod(str, mod, char) {
return char.repeat((mod - (str.length % mod)) % mod) + str;
}
function padEndToMod(str, mod, char) {
return str + char.repeat((mod - (str.length % mod)) % mod);
}
// base 16(hex) functions
const base16Chars = "0123456789ABCDEF";
@curse-github
curse-github / sha512.cpp
Created January 15, 2025 15:26
ascii sha512 algorithm
// adapted from my own sha256 algorithm and the instructions for conversion found at https://en.wikipedia.org/wiki/SHA-2
// note: this only works on ascii text (which is the default in c++), and wouldnt working on anything like a wstring, or buffer of some kind
#include <string>// for std::string
#include <vector>// for std::vector<T>
#include <cmath>// for std::pow
const unsigned long long int maxWord = 1ULL<<32;
#include <iostream>
#include <iomanip>
std::string sha512(std::string str, bool debug) {
@curse-github
curse-github / md5.cpp
Created January 15, 2025 15:26
ascii md5 algorithm
// adapted mostly from pseudo-code algorithm at https://en.wikipedia.org/wiki/MD5
// with some slight help from chat-gpt, because I forgot about the little endian parts
// note: this only works on ascii text (which is the default in c++), and wouldnt working on anything like a wstring, or buffer of some kind
// from my tests this took roughly 0.025 milliseconds on average even on strings with a length of 500 characters
#include <string>// for std::string
#include <vector>// for std::vector<T>
#include <cmath>// for std::pow
const unsigned long long int maxWord = std::pow(2, 32);
std::string md5(std::string str) {