Swift から CCKeyDerivationPBKDF を呼び出そうとしています。
Project-Bridging-Header.h に必要なヘッダーをインポートしました。
#import <CommonCrypto/CommonKeyDerivation.h>
(ちなみに、ブリッジ ヘッダーは、私のプロジェクトで他の Objective C コードをインポートするために正しく機能しているようです)。
Xcode では、.swift ファイルから次の定義にジャンプできます。
int
CCKeyDerivationPBKDF( CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
const uint8_t *salt, size_t saltLen,
CCPseudoRandomAlgorithm prf, uint rounds,
uint8_t *derivedKey, size_t derivedKeyLen)
最後に、次のように関数を呼び出そうとすると:
let result = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), NSString(password).UTF8String, size_t(passwordLength), UnsafePointer<UInt8>(salt.bytes), size_t(salt.length), CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), uint(actualRoundCount), UnsafeMutablePointer<UInt8>(derivedKey.mutableBytes), size_t(derivedKey.length));
...次のコンパイラ エラーが発生します。
タイプ '(CCPBKDFAlgorithm, UnsafePointer, size_t, UnsafePointer, size_t, CCPseudoRandomAlgorithm, uint, UnsafeMutablePointer, size_t)' の引数リストで 'init' を呼び出すことはできません
私はすべてのキャストが正しいと信じています (実際、コンパイラ エラーは、各パラメーターの各問題を特定するのに役立ちました) - これにより、コンパイラは CCKeyDerivationPBKDF を呼び出す意図を理解していると思います。
ただし、他のすべてのキャスト エラーが解消された後、コンパイラは混乱し、初期化子を使用してクラスを構築しようとしていると認識します。
誰かが私のやり方の誤りを教えてくれることを願っています。
(Xcode 6 ベータ 7)
要求に応じて、コンテキスト内の完全なコード:
class func generateAesKeyForPassword(password: String, salt: NSData, roundCount: UInt?, error: NSErrorPointer) -> (key: NSData, actualRoundCount: UInt)?
{
let derivedKey = NSMutableData(length: kCCKeySizeAES256)
let passwordLength = size_t(password.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
var actualRoundCount: UInt
if roundCount != nil
{
actualRoundCount = roundCount!
}
else
{
actualRoundCount = UInt(CCCalibratePBKDF(CCPBKDFAlgorithm(kCCPBKDF2), passwordLength, UInt(salt.length), CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), UInt(derivedKey.length), UInt32(300) /* milliseconds */));
}
let result = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), NSString(password).UTF8String, size_t(passwordLength), UnsafePointer<UInt8>(salt.bytes), size_t(salt.length), CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), uint(actualRoundCount), UnsafeMutablePointer<UInt8>(derivedKey.mutableBytes), size_t(derivedKey.length));
if result != 0
{
let errorDescription = "CCKeyDerivationPBKDF failed with error: '\(result)'"
error.memory = MyError(domain: ClientErrorType.errorDomain, code: Int(result), descriptionText: errorDescription)
return nil
}
return (NSData(data: derivedKey), actualRoundCount)
}