Skip to content

Instantly share code, notes, and snippets.

@darelf
darelf / typing.js
Created March 8, 2017 15:56
Minimal Javascript "typing" effect
function type_line(tlen, text, target, timing) {
var txt = text.substr(0, tlen++)
target.innerHTML = txt
if (tlen < text.length + 1) {
setTimeout(type_line.bind(null, tlen, text, target), timing)
}
}
@darelf
darelf / jwt.py
Last active April 13, 2022 21:24
Python 3 JWT
"""
Functions for creating and verifying authentication tokens
according to the `JWT spec <https://jwt.io/>`_.
JWT authentication tokens are made of three sections that are
Base64Url encoded with no padding. The third section is an HMAC
of the first two sections, so that without knowing the secret
key you cannot verify the token nor create tokens that will be
accepted on the other end.
@darelf
darelf / base64.cpp
Last active August 28, 2023 17:44
Base64 url encode/decode. C++ adapted from some old code by someone else...
#include <string>
#include <vector>
/*
Base64 translates 24 bits into 4 ASCII characters at a time. First,
3 8-bit bytes are treated as 4 6-bit groups. Those 4 groups are
translated into ASCII characters. That is, each 6-bit number is treated
as an index into the ASCII character array.
If the final set of bits is less 8 or 16 instead of 24, traditional base64
@darelf
darelf / base64_url.c
Last active January 18, 2017 15:14
Do base64url encoding with https://kore.io
#include <kore/kore.h>
#include <string.h>
char * base64_url_decode(char * in) {
size_t len;
char * padded;
u_int8_t * out;
unsigned int i;
len = strlen(in);
@darelf
darelf / consumer.erl
Last active December 22, 2016 19:15
Consume DataCite API data
-module(consumer).
%% API
-export([execute_query/0]).
parse_item(Data) ->
ID = maps:get(<<"id">>, Data),
Item = maps:get(<<"attributes">>, Data),
DOI = maps:get(<<"doi">>, Item),
Title = maps:get(<<"title">>, Item),
Published = maps:get(<<"published">>, Item),
@darelf
darelf / UserAuthTest.java
Created December 16, 2016 14:19
Sample unit test function for UserAuth.java
@Test
public void testJWT() throws Exception {
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, 20);
JsonObject obj = Json.object();
obj.add("sub", "1234567890");
obj.add("name", "John Doe");
obj.add("admin", true);
obj.add("exp", now.getTimeInMillis());
@darelf
darelf / UserAuth.java
Last active December 6, 2016 13:45
Example of JWT tokens with Apache Commons and EclipseSource JSON libraries
import java.util.Calendar;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
@darelf
darelf / display.erl
Last active November 19, 2015 19:02
Format File Sizes
-module(display).
-export([format/1,format/2]).
format(Num) -> format(Num, 2).
format(Num, Precision) ->
Abs = abs(Num),
TB = math:pow(10,12),
GB = math:pow(10,9),
MB = math:pow(10,6),
@darelf
darelf / prototransform.js
Last active August 29, 2015 14:03
Trying to figure out stream Transform with protocol buffers
var msg1 = { msg: 'commit' }
var msg2 = { msg: 'update', id: 5, length: 21 }
var protobuf = require('protocol-buffers')
var schema = protobuf([
{ "name": "msg", "type": "string" },
{ "name": "length", "type": "float" },
{ "name": "id", "type": "float" }
])
var encoder = schema.encode
@darelf
darelf / ostixml.js
Last active August 29, 2015 13:58
Access OSTI xml data and parse the results
/*
* OSTI is home to lots of scientific, engineering and technical journals and other doucments.
* It is part of the US Dept. of Energy. This shows how to retrieve search results from their vast
* collection of data as XML and consume it.
*/
var xml2js = require('xml2js')
var http = require('http')
// Get 100 entries from OSTI in the subject area HYDROGEN
// See https://www.osti.gov/home/sites/www.osti.gov.home/files/SciTechXMLDataServices1.1.pdf for more info