Skip to content

Instantly share code, notes, and snippets.

View cky's full-sized avatar

C. K. Young cky

View GitHub Profile
@cky
cky / csp2pem.rb
Last active December 29, 2022 23:26
Quick script for converting PEM-format RSA keys to a format usable by .NET's RSA.FromXmlString
#!/usr/bin/ruby
require 'openssl'
TYPES = {
6 => {magic: 'RSA1', private_key: false},
7 => {magic: 'RSA2', private_key: true}
}
data = ARGV[0] ? File.binread(ARGV[0]) : $stdin.binmode.read
type, version, algo, magic, bits, rest = data.unpack('ccx2l<a4l<a*')
@cky
cky / gist:8500450
Last active June 21, 2022 02:25
Guile implementation of Clojure's `#(...)` reader macro, but using `##(...)` instead to avoid conflicting with vectors.
(use-srfis '(1 69))
(read-hash-extend #\#
(lambda (c port)
(define ht (make-hash-table eqv?))
(define (ht-ref key)
(hash-table-ref ht key (lambda ()
(define sym (gensym))
(hash-table-set! ht key sym)
sym)))
(define (hash-key x)
@cky
cky / gist:666001
Created November 7, 2010 07:19
CipherSaber code golf

Find the shortest way to write CipherSaber. There are several parts to this puzzle:

RC4/Arcfour

Arcfour is fully specified elsewhere, but for completeness, I'll describe it here.

Key setup

Set up two arrays, S and S2, both of length 256, where k_1 is the first byte of the key, and k_n is the last.

@cky
cky / sha12.scm
Last active June 12, 2021 14:00
Scheme implementation of SHA1 and SHA2
;;;; SHA-1 and SHA-2 implementations.
;;;; Uses R7RS bytevector and byte I/O interfaces.
;;;; Requires SRFIs 1, 26, 43, and 60.
;;; Auxiliary definitions to avoid having to use giant tables of constants.
(define primes80 '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157
163 167 173 179 181 191 193 197 199 211 223 227 229 233 239
241 251 257 263 269 271 277 281 283 293 307 311 313 317 331
@cky
cky / r1-log.md
Last active January 17, 2018 08:17
100 Days of Code challenge, round 1

#100DaysOfCode Log—Round 1—Chris Jester-Young

The log of my #100DaysOfCode challenge. Started on 1 January 2018.

Log

R1D1 (2018-01-01)

Created [SwiftJson] as a Swift port of [ktjson] and [c++17-json]. Coded up the basic JsonValue enum type.

@cky
cky / flatten.js
Created June 12, 2016 15:50
One way to flatten in ES6, not necessarily the best
function flatten(x) {
var result = [];
var appendToResult = function (xs) {
result.splice(result.length, 0, ...xs);
}
if (Array.isArray(x)) {
x.map(flatten).forEach(appendToResult);
} else {
result.push(x);
}
@cky
cky / miller-rabin.rkt
Last active June 2, 2016 22:22
My Miller-Rabin implementation (see http://codereview.stackexchange.com/a/129991/216 for context)
#lang racket
(define (expmod base exp m)
(let recur ([exp exp])
(cond [(zero? exp) 1]
[(even? exp) (let* ([prev (recur (quotient exp 2))]
[result (modulo (sqr prev) m)])
(if (and (< 1 prev (sub1 m))
(= result 1))
0
result))]
@cky
cky / area.rkt
Last active March 28, 2016 16:11
HackerRank Lambda Calculi March 2016: cky's answers
#lang racket
(define (determinant p1 p2)
(- (* (car p1) (cdr p2))
(* (cdr p1) (car p2))))
(define points (read))
(define first (cons (read) (read)))
(define-values (area last)
(for/fold ([area 0] [last first])
([i (in-range (sub1 points))]
@cky
cky / pollute.rkt
Last active January 4, 2016 04:59
Gauche-style ^ macros for all combinations of one and two letters. Hi, Mark Weaver!
(define-syntax pollute-my-namespace
(let ((letters "abcdefghijklmnopqrstuvwxyz_")
(char->symbol (compose string->symbol string)))
(define (make-macros stx)
(lambda ()
(for*/lists (ids syntaxes)
((x letters)
(y letters))
(if (char=? x y)
(values (datum->syntax stx (char->symbol #\^ x))
@cky
cky / gist:7533352
Created November 18, 2013 19:01
Stack Overflow [code-golf] tag info