     }
 
     private func hmac(algorithm: HMAC.Algorithm, key: String, message: String, expectedHMAC: String) {
-        let key = key.dataUsingEncoding(NSUTF8StringEncoding)!
-        let message = message.dataUsingEncoding(NSUTF8StringEncoding)!
-        let hmac = message.hmac(algorithm, key: key).hexString()
+        let keyData = key.dataUsingEncoding(NSUTF8StringEncoding)!
+        let messageData = message.dataUsingEncoding(NSUTF8StringEncoding)!
+        let hmac = messageData.hmac(algorithm, key: keyData).hexString()
         XCTAssertEqual(hmac, expectedHMAC)
+        XCTAssertEqual(hmac, message.hmac(algorithm, key: key))
     }
 
     // MARK: PBKDF
 
     func testDerivedKey_SHA1() {
+        calibrate(Consts.key, saltLength: Consts.pbkdfSalt.dataUsingEncoding(NSUTF8StringEncoding)!.length, pseudoRandomAlgorithm: .SHA1, derivedKeyLength: 16, msec: 100)
         PBKDF2(Consts.key, salt: Consts.pbkdfSalt, pseudoRandomAlgorithm: .SHA1, rounds: 20, derivedKeyLength: 16, expectedDerivedKey: Consts.pbkdfSHA1)
     }
 
     func testDerivedKey_SHA256() {
+        calibrate(Consts.key, saltLength: Consts.pbkdfSalt.dataUsingEncoding(NSUTF8StringEncoding)!.length, pseudoRandomAlgorithm: .SHA256, derivedKeyLength: 32, msec: 100)
         PBKDF2(Consts.key, salt: Consts.pbkdfSalt, pseudoRandomAlgorithm: .SHA256, rounds: 20, derivedKeyLength: 32, expectedDerivedKey: Consts.pbkdfSHA256)
     }
 
     func testDerivedKey_SHA512() {
+        calibrate(Consts.key, saltLength: Consts.pbkdfSalt.dataUsingEncoding(NSUTF8StringEncoding)!.length, pseudoRandomAlgorithm: .SHA512, derivedKeyLength: 64, msec: 100)
         PBKDF2(Consts.key, salt: Consts.pbkdfSalt, pseudoRandomAlgorithm: .SHA512, rounds: 20, derivedKeyLength: 64, expectedDerivedKey: Consts.pbkdfSHA512)
     }
 

        static let aesCiphertext_CBC_256_IV = "O49HzhxdRYMgzqQYx+pECj98Mxn+EtFy7ZnBE9yr+fPZXwgzWaLnLasoKszifIlHRnY3W0ehdWT2Zysaguorcw=="
    }

    // MARK: Digest

    func testMD2() {
        digest(Consts.message, expectedDigest: Consts.MD2) { $0.MD2() }
    }

    func testMD4() {
        digest(Consts.message, expectedDigest: Consts.MD4) { $0.MD4() }
    }

    func testMD5() {
        digest(Consts.message, expectedDigest: Consts.MD5) { $0.MD5() }
    }

    func testSHA224() {
        digest(Consts.message, expectedDigest: Consts.SHA224) { $0.SHA224() }
    }

    func testSHA256() {
        digest(Consts.message, expectedDigest: Consts.SHA256) { $0.SHA256() }
    }

    func testSHA384() {
        digest(Consts.message, expectedDigest: Consts.SHA384) { $0.SHA384() }
    }

    func testSHA512() {
        digest(Consts.message, expectedDigest: Consts.SHA512) { $0.SHA512() }
    }

    private func digest(message: String, expectedDigest: String, algorithm: (NSData) -> NSData) {
        let message = message.dataUsingEncoding(NSUTF8StringEncoding)!
        let digest = algorithm(message).hexString()
        XCTAssertEqual(digest, expectedDigest)
    }


        let aesKey = NSData(base64EncodedString: key, options: [])!
        let IV = IV == nil ? nil : NSData(base64EncodedString: IV!, options: [])
        let ciphertext = try! plaintext.encrypt(algorithm, options: options, key: aesKey, iv: IV)
        let plaintextProof = try! ciphertext.decrypt(algorithm, options: options, key: aesKey, iv: IV)
        XCTAssertEqual(plaintext, plaintextProof)
        XCTAssertEqual(ciphertext.base64EncodedStringWithOptions([]), expectedCyphertext)
    }

