Skip to content

Instantly share code, notes, and snippets.

@unix-beard
Last active August 29, 2015 14:13
Show Gist options
  • Save unix-beard/8b77837d62d1e93a179a to your computer and use it in GitHub Desktop.
Save unix-beard/8b77837d62d1e93a179a to your computer and use it in GitHub Desktop.
Convert hex to base64
(* The matasano crypto challenges
* http://cryptopals.com/sets/1/challenges/1/
*
* Compile it like this:
* $ ocamlfind ocamlc -package base64,batteries -o hexbase64 -linkpkg hexbase64.ml
*)
open Batteries
open B64
let s = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
let pair_elem i ch =
if i mod 2 <> 0 && i <> 0 then
let hex = int_of_string ("0x" ^ String.make 1 (String.get s (i - 1)) ^ String.make 1 (String.get s i)) in
char_of_int hex
else
Char.chr 0
let () =
let decoded_str = BatString.filter ((<>) (Char.chr 0)) (BatString.mapi pair_elem s) in
print_endline (B64.encode decoded_str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment