Skip to content

Instantly share code, notes, and snippets.

View bozdoz's full-sized avatar
☠️
I may be dead

bozdoz bozdoz

☠️
I may be dead
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bozdoz
bozdoz / getPassword.js
Created June 4, 2024 00:18
Get a password from stdin
const readline = require("node:readline");
const { Writable } = require("node:stream");
const getPassword = (user) =>
new Promise((resolve) => {
const mutableStdout = new Writable({
write: function (chunk, encoding, callback) {
if (!this.muted) process.stdout.write(chunk, encoding);
callback();
},
@bozdoz
bozdoz / draft.js
Last active February 28, 2024 02:48
Node.js Encryption/Decryption Example
const crypto = require("node:crypto");
const { Buffer } = require("node:buffer");
const ALGO = "aes-256-cbc";
// random SECRET stored in memory
const SECRET = crypto.randomBytes(64);
function getKey(salt) {
// In 2023, OWASP recommended to use 600,000 iterations for PBKDF2-HMAC-SHA256 and 210,000 for PBKDF2-HMAC-SHA512.[6]
return crypto.pbkdf2Sync(SECRET, salt, 210_000, 32, "sha512");
@bozdoz
bozdoz / day-01.rs
Created November 28, 2023 05:14
2019 Advent of Code
// to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
use std::fs;
use std::io::Error;
fn main() -> Result<(), Error> {
let contents = fs::read_to_string("./src/input.txt");
let data = match contents {
Ok(c) => c,
@bozdoz
bozdoz / useWhyDidYouUpdate.ts
Last active July 5, 2023 20:15
useWhyDidYouUpdate
import { useRef, useEffect } from 'react';
type IProps = Record<string, unknown>;
const useWhyDidYouUpdate = (componentName: any, props: any) => {
const oldPropsRef = useRef<IProps>({});
useEffect(() => {
if (oldPropsRef.current) {
// iterate through all the key of the old and new props
@bozdoz
bozdoz / exclusive_enums.go
Created November 6, 2022 14:57
exclusive_enums.go
package main
type dessert int
const (
ICE_CREAM dessert = iota
CUP_CAKES
GUMMY_WORMS
)
@bozdoz
bozdoz / remote-diverged.sh
Last active August 1, 2023 13:47
find remote branches that have diverged by some amount of commits
git ls-remote --heads origin | while read sha ref; do
behind=`git rev-list $sha..master --count`
if [ $behind -ge 1000 ]; then
echo "$ref $behind";
fi
done
[alias]
rebase-since = !sh -c 'git rebase -i $(git merge-base --fork-point \"${1:-master}\" HEAD)' -
graph = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue) <%an>%Creset' --abbrev-commit --date=relative
@bozdoz
bozdoz / getElemByText.js
Created April 25, 2021 03:57
JavaScript document XPath text selector
function getElemByText(text) {
return document.evaluate(
`//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '${text}')]`,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
@bozdoz
bozdoz / defaultProps.d.ts
Last active April 19, 2021 16:53
Defining DefaultProps with HOC
import React from 'react';
const withTheme = <P extends {}>(Comp: React.ComponentType<P>) => {
return Comp as any as React.ForwardRefExoticComponent<
React.PropsWithoutRef<P> &
React.RefAttributes<any>
> & {
defaultProps: typeof Comp['defaultProps'];
};
}