       encryptedKey = raw.slice 16, 16 + recipientKeyKytes
       @recipientKey.decrypt(encryptedKey)
     catch
-      throw new Error "Incorrect message length."
+      throw new DecryptionError "Incorrect message length."
 
   getDecryptedMessage: (iv, key, raw) ->
     recipientKeyKytes = @recipientKey.keyPair.cache.keyBitLength / 8

    encryptedMessage = @encryptMessage signedMessage, message, key, iv
    new Buffer(iv + encryptedKey + encryptedMessage).toString "base64"

exports.Decryption = class Decryption

  constructor: (@recipientKey, @senderKey) ->


  verify: (signature, message) ->
    hash = crypto.createHash("sha512").update(message).digest()
    @senderKey.verify hash, signature

class InvalidMessageException extends Error

class DecryptionError extends Error

class EncryptionError extends Error

module.exports =
  Encryption: Encryption
  Decryption: Decryption
  InvalidMessageException: InvalidMessageException
  DecryptionError: DecryptionError
  EncryptionError: EncryptionError
