I am currently trying to make my own password manager application similar to master password, which uses an algorithm to generate passwords so they do not have to be stored on the clients computer or online.
To achieve this I have decided to use the ChaCha20 cipher algorithm using the CryptoSwift library. Here is the code I have at the moment (OS X Application):
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
}
}
The line where I am getting the error on is let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
. The error that I am getting is "Fatal error: unexpectedly found nil while unwrapping an Optional value". This is probably due to the the '!' before the encrypt method since everything else works before that. I have tried replacing the '!' with a '?', however the variable encrypted
is then equal to nil, if I remove the '!' or '?' altogether I get a syntax error.
How would I fix the issue on the let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
line?