Skip to content

Instantly share code, notes, and snippets.

Carl Johnson carlmjohnson

Block or report user

Report or block carlmjohnson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@carlmjohnson
carlmjohnson / crypto-rand.js
Last active May 28, 2020 — forked from paragonie-scott/gist:c7a73fd0f759e451cf07
Javascript CSPRNG for Integers
View crypto-rand.js
function secureRand(min, max) {
let range = max - min;
if (range < 2) {
return min;
}
let bits_needed = Math.ceil(Math.log2(range));
if (bits_needed > 31) {
throw new Error("cannot generate numbers larger than 31 bits.");
}
@carlmjohnson
carlmjohnson / tot
Last active Feb 28, 2020 — forked from zrzka/!IMPORTANT.md
A shell script for Tot
View tot
#!/usr/bin/env bash
# Fork of gruber's tot.sh https://gist.github.com/gruber/b18d8b53385fa612713754799ed4d0a2
# which is a fork of chockenberry's tot.sh https://gist.github.com/chockenberry/d33ef5b6e6da4a3e4aa9b07b093d3c23
#
# WARNING some options have different & potentially destructive meaning, for example:
# -r gets the dot contents in original script, but this one REPLACES the dot contents
# and does use -p to get the dot contents
#
@carlmjohnson
carlmjohnson / random.js
Created Jan 24, 2020
Generate random hex digits
View random.js
function randomHex(n) {
let bytes = new Uint8Array(n);
crypto.getRandomValues(bytes);
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
}
View Article Bits.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View credit-card.vue
<template>
<div id="app">
Value is "{{ value }}". <br />
<input v-model="value" @input="handleChange($event)" />
</div>
</template>
<script>
function trimAndSpace(val) {
return val
@carlmjohnson
carlmjohnson / xtract.py
Created Sep 3, 2019
For SVG files with bloated PNG embeds
View xtract.py
def xtract_pngs_from_svgs(name):
fname = name +'.svg'
#
with open(fname) as f:
raw = f.read()
#
prefix = 'xlink:href="data:image/png;base64,'
suffix = '"'
chunks = []
found = []
View utils.js
const onAnimate = (eventType, callback) => {
let timeout;
window.addEventListener(eventType, ev => {
if (timeout) {
window.cancelAnimationFrame(timeout);
}
timeout = window.requestAnimationFrame(callback);
});
};
View combinations.go
package main
import (
"fmt"
"strings"
)
func Combinations(n int, combs []int) (next func() bool) {
k := len(combs)
if k == 0 || n < k {
View As() and Is() are useful for optional interfaces beyond error.md

EDIT: On further thought, the implications of using iface.As, e.g., for io.WriterTo are quite subtle. If you were trying, say, to add logging to all Read calls, you wouldn't want to simply pass through to the underlying io.WriterTo because the call would go unlogged. Instead you'd need to implement the As method on your type so as to only return a WriterTo under certain conditions. Still, I think this idea is worth exploring more.


The Go 2 process has produced a proposal for extending the errors standard package, which is currently in testing.

My observation about the experimental errors package is: As() and Is() solve problems that exist well beyond the error interface.

Essentially the problem is this, many packages begin by requiring a certain interface, such as error, io.Reader, or

View html-scan.go
package main
import (
"bufio"
"bytes"
"fmt"
)
const html = `
<html>
You can’t perform that action at this time.