Skip to content

Instantly share code, notes, and snippets.

View dchest's full-sized avatar
☮️

Dmitry Chestnykh dchest

☮️
View GitHub Profile
@dchest
dchest / salsa20.js
Last active November 9, 2020 02:48
/*
IMPORTANT!!! DO NOT USE THIS. It works, but you'll probably get it wrong,
because it must be keyed with at least 128 bits of entropy, and where
do you get this entropy, huh?
- In a browser, you get it from window.crypto.getRandomValues().
- In Node, you get it from crypto.randomBytes()
Now LOOK AT YOU! You already have secure ways to generate random bytes,
@dchest
dchest / uuid.php
Created February 15, 2020 19:26
UUIDv4 generator for PHP 7+
<?php
/**
* Generate a random UUIDv4.
* @return string UUID
*/
function generate_uuid()
{
$b = random_bytes(16);
$b[6] = chr(ord($b[6]) & 0x0f | 0x40);
// Note: this is for scrypt implementation, which doesn't use diagonal constants,
// so the order of loading from x is different than in normal Salsa20 implementation.
function salsa20_8(x) {
var j0 = (x[ 0] & 0xff) | ((x[ 1] & 0xff)<<8) | ((x[ 2] & 0xff)<<16) | ((x[ 3] & 0xff)<<24);
var j1 = (x[ 4] & 0xff) | ((x[ 5] & 0xff)<<8) | ((x[ 6] & 0xff)<<16) | ((x[ 7] & 0xff)<<24);
var j2 = (x[ 8] & 0xff) | ((x[ 9] & 0xff)<<8) | ((x[10] & 0xff)<<16) | ((x[11] & 0xff)<<24);
var j3 = (x[12] & 0xff) | ((x[13] & 0xff)<<8) | ((x[14] & 0xff)<<16) | ((x[15] & 0xff)<<24);
var j4 = (x[16] & 0xff) | ((x[17] & 0xff)<<8) | ((x[18] & 0xff)<<16) | ((x[19] & 0xff)<<24);
var j5 = (x[20] & 0xff) | ((x[21] & 0xff)<<8) | ((x[22] & 0xff)<<16) | ((x[23] & 0xff)<<24);
@dchest
dchest / cryptopass.py
Created December 16, 2010 02:56
CryptoPass implementation in Python
# Cryptopass by dchest
# Based on:
# pbkdf2.py -- library to calculate keys from passwords
# Copyright (C) 2010 Tobias Ammann
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
@dchest
dchest / randomString.js
Last active December 22, 2019 08:19
Generates cryptographically secure uniform random string in browsers and Node.js [IN DEVELOPMENT]
// randomString(length)
// --------------------
//
// Generates and returns a cryptographically secure
// uniform alphanumeric random string.
//
// Examples:
//
// randomString(14) // "oXYWpc1vODNR3M"
// randomString.hex(8) // "663c722b65943b9b"
@dchest
dchest / utils.js
Last active August 2, 2019 11:41
Short Utils
// String ranges: strange('A-Za-z0-9+/') -> "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy012345678+/"
const strange = s => s.replace(/(.)-(.)/g, (_, x, y) => { for (var i = x.charCodeAt(0) + 1; i < y.charCodeAt(0); i++) x += String.fromCharCode(i); return x })
// Base64 encoding of Uint8Array of ArrayBuffer and decoding to Uint8Array
const encodeBase64 = a => btoa(Array.from(a instanceof ArrayBuffer ? new Uint8Array(a) : a).map(x => String.fromCharCode(x)).join(''));
const encodeBase64url = a => encodeBase64(a).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); // no padding
const decodeBase64 = s => Uint8Array.from(atob(s), x => x.charCodeAt(0));
// TODO: decodeBase64url
@dchest
dchest / gimli.js
Last active June 2, 2019 22:41
Gimli permutation in JavaScript. EXPERIMENTAL VERSION
/** Gimli permutation - https://gimli.cr.yp.to */
function gimli(s) {
var r, x, y, z,
a = s[ 0] | s[ 1] << 8 | s[ 2] << 16 | s[ 3] << 24,
b = s[ 4] | s[ 5] << 8 | s[ 6] << 16 | s[ 7] << 24,
c = s[ 8] | s[ 9] << 8 | s[10] << 16 | s[11] << 24,
d = s[12] | s[13] << 8 | s[14] << 16 | s[15] << 24,
e = s[16] | s[17] << 8 | s[18] << 16 | s[19] << 24,
f = s[20] | s[21] << 8 | s[22] << 16 | s[23] << 24,
g = s[24] | s[25] << 8 | s[26] << 16 | s[27] << 24,
@dchest
dchest / pure-tweaks.css
Last active April 21, 2019 11:50
PureCSS tweaks
/*
dchest's PureCSS Tweaks
https://gist.github.com/dchest/500746c2e41baaafcab3
*/
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
@dchest
dchest / fossil-add
Created January 16, 2011 13:51
fossil add doesn't ignore files specified in ignore-glob. This uses fossil-extra to add any extra files in repo to fossil (because extra uses ignore-glob).
#!/bin/sh
fossil extra | tr '\n' '\0' | xargs -0 -t fossil add
@dchest
dchest / meet.c
Created July 3, 2011 18:10
Example of meet-in-the-middle attack (in C)
/* Written by Dmitry Chestnykh. Public domain. */
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <openssl/blowfish.h>
#include <openssl/lhash.h>
/* Encryption and decryption */