我目前正在尝试制作类似于master password的自己的密码管理器应用程序,它使用一种算法来生成密码,因此它们不必存储在客户端计算机上或在线上。
为了实现这一点,我决定使用使用CryptoSwift库的 ChaCha20 密码算法。这是我目前拥有的代码(OS X 应用程序):
import Cocoa
import CryptoSwift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
do
{
let UInt8String = "Test".utf8
print("UTF8 string: \(UInt8String)")
let UInt8Array = Array(UInt8String)
print("UTF8 array: \(UInt8Array)")
let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
print("Encrypted data: \(encrypted)")
} catch _ {
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
我得到错误的那一行是let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
. 我得到的错误是“致命错误:在展开可选值时意外发现 nil”。这可能是由于'!' 在加密方法之前,因为在此之前一切正常。我试过替换'!' 带有“?”,但是encrypted
如果我删除“!”,则变量等于nil 或者 '?' 我完全得到一个语法错误。
我将如何解决let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
在线问题?