+zc.pad_base64 = (text) ->
+  while text.length % 4 != 0
+    text = text + '='
+  return text
+
 zc.format_pem = (key, title) ->
   rv = "-----BEGIN " + title + "-----\n"
-  while key.length % 4 != 0
-    key = key + '='
   while key
     rv += key.slice(0, 64) + "\n"
     key = key.slice(64)

  k = new RSAKey()
  k.generate(size, '10001')
  key = k.privateKeyToPkcs1PemString()
  _.defer(callback, zc.format_pem(key, "RSA PRIVATE KEY"))


zc.get_public_key = (private_key) ->
  key = new RSAKey()
  key.readPrivateKeyFromPEMString(private_key)
  public_key = key.publicKeyToX509PemString()
  return zc.format_pem(public_key, "PUBLIC KEY")


class zc.Crypto

  sign: (data, callback) ->
    @create_crypt (crypt) ->
      crypt.sign data, (_, signed) ->
        callback(zc.pad_base64(signed.signature))

  verify: (data, signature, callback) ->
    @create_crypt (crypt) ->
