/// <summary> /// attempts to read a file into cryptonote format /// </summary> /// <param name="fileName">full path to file</param> /// <param name="note">output note</param> /// <returns>true if successful</returns> public bool TryRead(string fileName, out CryptoNote note) { using var fileStream = File.OpenRead(fileName); bool success; index = 0; try { note = new CryptoNote(0); var fileSize = new FileInfo(fileName).Length; var version = BitConverter.ToUInt16(ReadBytes(fileStream, VersionLength)); if (version != Constants.ProtocolVersion) { return(false); } note.Salt = ReadBytes(fileStream, SaltLength); note.Iterations = BitConverter.ToInt32(ReadBytes(fileStream, 4)); note.InitializationVector = ReadBytes(fileStream, IvLength); note.ValidityCheck = ReadBytes(fileStream, ValidityLength); note.Cipher = ReadBytes(fileStream, (int)(fileSize - index)); success = true; } finally { fileStream.Close(); } return(success); }
/// <summary> /// Creates of overwrites a fileContents at the specified path with encrypted data in the supplied cryptoNoteFile /// </summary> /// <param name="fileContentsContents"></param> /// <param name="path"></param> public static void SaveToFile(CryptoNote note, string path) { using var fileStream = File.Open(path, FileMode.OpenOrCreate); var fileContents = new object[] { Constants.ProtocolVersion, note.Salt, note.Iterations, note.InitializationVector, note.ValidityCheck, note.Cipher, }; foreach (var item in fileContents) { WriteToFile(fileStream, item); } fileStream.Close(); }