Skip to content

dev

tools for programmers: login tokens, URLs, version numbers and file permissions, plus networks, IDs, colors, binaries and hardware

Small tools for programming work. They cover login tokens, URLs, version numbers and file permissions, and the submodules add networking, IDs, colors, binary files and hardware.

  • jwt decode a JSON Web Token, the login token many websites and APIs send, into {header, payload, signature}
  • jwt_sign sign claims as a JSON Web Token with an HMAC key, which jwt reads back
  • jwt_verify whether a JSON Web Token’s HMAC signature matches the key
  • url_parse split a URL into its parts: {scheme, host, port, path, query, fragment}
  • url_build put a URL together from a map of its parts, the reverse of url_parse
  • semver parse a semantic version like “1.2.3-beta+5” into {major, minor, patch, pre, build}
  • bump the next version
  • chmod convert Unix file permissions between the number form, like 755, and the letters ls -l shows, like “rwxr-xr-x”
jwt(s: str) -> map

decode a JSON Web Token, the login token many websites and APIs send, into {header, payload, signature}

The payload holds claims like who the user is, and its exp, iat and nbf times (expires, issued at, not before) become dates. The signature is not checked, so use this to read a token, never to decide whether to trust one

1
jwt("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZGEiLCJleHAiOjE5MjQ5OTIwMDB9.c2ln").payload.exp2030-12-31 18:00:00 -06:00
Example outputCtrl+Enter to run

Tags: crypto

See also decode, hmac

jwt_sign(claims: map, key: str, alg: str = “HS256”) -> str

sign claims as a JSON Web Token with an HMAC key, which jwt reads back

alg is “HS256”, “HS384” or “HS512”. Dates under exp, iat and nbf become Unix seconds, as tokens store them

1
jwt_sign({sub: "ada", admin: true}, "secret")"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZGEiLCJhZG1pbiI6dHJ1ZX0.LG5jqWCrAAWbUMv6IIF_kHEjmYwSQrtDQajAELdwFLs"
2
jwt(jwt_sign({sub: "ada", exp: now + 1 h}, "secret")).payload{sub: "ada", exp: 2026-11-04 13:12:41 -06:00}
Example outputCtrl+Enter to run

Tags: crypto

See also jwt, jwt_verify, hmac

jwt_verify(token: str, key: str) -> bool

whether a JSON Web Token’s HMAC signature matches the key

Only HS256, HS384 and HS512 tokens can match, so one with alg “none” or anything else is false. The exp and nbf times aren’t checked, so look at jwt(token).payload too

1
jwt_verify(jwt_sign({sub: "ada"}, "secret"), "secret")true
2
jwt_verify(jwt_sign({sub: "ada"}, "secret"), "guess")false
Example outputCtrl+Enter to run

Tags: crypto

See also jwt, jwt_sign

url_parse(s: str) -> map

split a URL into its parts: {scheme, host, port, path, query, fragment}

The query string becomes a map, and a key that appears more than once gets a list of its values

1
url_parse("https://example.com:8080/a/b?x=1&y=two%20words#top"){scheme: "https", host: "example.com", port: 8080, path: "/a/b", query: {x: "1", y: "two words"}, fragment: "top"}
Example outputCtrl+Enter to run

Tags: parsing

See also url_build, encode

url_build(m: map) -> str

put a URL together from a map of its parts, the reverse of url_parse

Parts that are missing are left out

1
url_build({scheme: "https", host: "example.com", path: "/search", query: {q: "a b"}})"https://example.com/search?q=a%20b"
Example outputCtrl+Enter to run

Tags: formatting

See also url_parse

semver(s: str) -> map

parse a semantic version like “1.2.3-beta+5” into {major, minor, patch, pre, build}

Three numbers, then an optional pre-release tag after - and build info after +. Compared as plain text, “1.10.0” sorts before “1.9.0”. The result compares by the versioning rules instead, against another semver or a plain version string, so 1.10.0 is newer and 2.0.0-rc.1 comes before 2.0.0

1
semver("1.2.3") < semver("1.10.0")true
2
semver("1.0.0-beta") < "1.0.0"true
3
semver("v2.1.0-rc.1+build5"){major: 2, minor: 1, patch: 0, pre: "rc.1", build: "build5"}
Example outputCtrl+Enter to run

Tags: parsing

See also bump

bump(v: str|map, part: str) -> str|map

the next version

From 1.2.3, “major” gives 2.0.0, “minor” 1.3.0 and “patch” 1.2.4. A pre-release of the version you’d bump to, like 2.0.0-rc.1 with “major”, becomes that release, 2.0.0

1
bump("1.2.3", "minor")"1.3.0"
2
bump("2.0.0-rc.1", "major")"2.0.0"
Example outputCtrl+Enter to run

See also semver

chmod(mode: int) -> str / chmod(mode: str) -> int

convert Unix file permissions between the number form, like 755, and the letters ls -l shows, like “rwxr-xr-x”

Each digit, or group of three letters, is read (r), write (w) and execute (x) for the file’s owner, its group, and everyone else

1
chmod(755)"rwxr-xr-x"
2
chmod("rw-r--r--")0o644
3
chmod(0o4755)"rwsr-xr-x"
Example outputCtrl+Enter to run

Tags: files

1
# what's in this token?
2
jwt("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZGEiLCJpYXQiOjE1MTYyMzkwMjJ9.c2ln").payload{sub: "ada", iat: 2018-01-17 19:30:22 -06:00}
3
# query string of a URL
4
url_parse("https://example.com/search?q=zil&page=2").query{q: "zil", page: "2"}
5
# is 1.10 newer than 1.9?
6
semver("1.10.0") > "1.9.3"true
7
# chmod 640 means
8
chmod(640)"rw-r-----"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.