internal static byte[] GenerateKeyFile(ITestOutputHelper output, IKeyCapture capture, IKeyFileService service) { lock (Sync) { var @out = new XunitDuplexTextWriter(output, Console.Out); var error = new XunitDuplexTextWriter(output, Console.Error); Assert.True(KeyFileManager.TryGenerateKeyFile(service.GetKeyFileStream(), @out, error, capture)); capture.Reset(); return(Crypto.SigningPublicKeyFromSigningKey(service, capture)); } }
public void Can_save_and_load_key_file_with_correct_password(string plaintext) { unsafe { var keyFilePath = Path.GetTempFileName(); var keyFileStream = File.Open(keyFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); var @out = new XunitDuplexTextWriter(_output, Console.Out); var error = new XunitDuplexTextWriter(_output, Console.Error); var generatedKeyFile = KeyFileManager.TryGenerateKeyFile(keyFileStream, @out, error, new PlaintextKeyCapture(plaintext, plaintext)); Assert.True(generatedKeyFile); keyFileStream.Dispose(); keyFileStream = File.OpenRead(keyFilePath); var loadedKeyFile = KeyFileManager.TryLoadKeyFile(keyFileStream, @out, error, out var secretKey, new PlaintextKeyCapture(plaintext, plaintext)); Assert.True(loadedKeyFile); NativeMethods.sodium_free(secretKey); } }
public void Cannot_load_saved_key_file_with_incorrect_password(string plaintext) { unsafe { var keyFilePath = Path.GetTempFileName(); var keyFileStream = File.Open(keyFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); var @out = new XunitDuplexTextWriter(_output, Console.Out); var error = new XunitDuplexTextWriter(_output, Console.Error); var generatedKeyFile = KeyFileManager.TryGenerateKeyFile(keyFileStream, @out, error, new PlaintextKeyCapture(plaintext, plaintext)); Assert.True(generatedKeyFile, nameof(generatedKeyFile)); keyFileStream.Dispose(); keyFileStream = File.OpenRead(keyFilePath); var loadedKeyFile = KeyFileManager.TryLoadKeyFile(keyFileStream, @out, error, out var secretKey, new PlaintextKeyCapture($"{plaintext}wrong", $"{plaintext}wrong")); Assert.False(loadedKeyFile, nameof(loadedKeyFile)); Assert.True(secretKey == default(byte *), nameof(secretKey)); } }