Skip to content

Instantly share code, notes, and snippets.

@zbroyar
Created March 8, 2011 21:04
Show Gist options
  • Save zbroyar/861054 to your computer and use it in GitHub Desktop.
Save zbroyar/861054 to your computer and use it in GitHub Desktop.
PKCS#1 ver 2 padding
let pkcs1v2pad key msg =
let blen = key.RSA.size/8
and mlen = String.length msg
and zero = char_of_int 0
and rnd = Random.string Random.secure_rng in
let padlen = blen - mlen in
if padlen > 3 then begin
let res = rnd padlen in
res.[0] <- (char_of_int 0);
res.[1] <- (char_of_int 2);
res.[padlen - 1] <- (char_of_int 0);
for i = 2 to padlen - 2 do
while res.[i] = zero do res.[i] <- (rnd 1).[0] done
done;
res ^ msg
end
else msg
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment