/// <summary> /// Initializes a new instance of the <see cref="FileMeta"/> class and generates missing keys. /// </summary> /// <param name="path">The local file path to load the meta data from.</param> /// <param name="algorithm">The cryptographic algorith to encrypt this <see cref="FileMeta"/>.</param> /// <param name="hmacKey">256 bit HMAC key to verify integrity of this <see cref="FileMeta"/>.</param> /// <param name="aesKey">256 bit AES key to encrypt this <see cref="FileMeta"/>.</param> /// <param name="fileKey">256 bit AES key to encrypt the associated file.</param> /// <exception cref="ArgumentException"/> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <exception cref="FileNotFoundException"/> public static async Task <FileMeta> FromFileAsync(string path, ContentAlgorithm algorithm, byte[] hmacKey, byte[] aesKey, byte[] fileKey) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException(nameof(path)); } if (algorithm != ContentAlgorithm.None && algorithm != ContentAlgorithm.Aes256CbcHmacSha256) { throw new ArgumentException("This content algorithm is not supported.", nameof(algorithm)); } FileMeta instance = new FileMeta(algorithm, hmacKey, aesKey, fileKey); await instance.LoadFromFileAsync(path); instance.Available = true; return(instance); }
private FileMeta(ContentAlgorithm algorithm, byte[] hmacKey, byte[] aesKey, byte[] fileKey) { Algorithm = algorithm; AesKey = aesKey; HmacKey = hmacKey; FileKey = fileKey; if (Algorithm == ContentAlgorithm.Aes256CbcHmacSha256) { if (AesKey == null) { AesKey = AesStatic.GenerateKey(); } else if (AesKey.Length != 32) { throw new ArgumentOutOfRangeException(nameof(aesKey)); } if (hmacKey == null) { HmacKey = AesStatic.GenerateKey(); } else if (HmacKey.Length != 32) { throw new ArgumentOutOfRangeException(nameof(hmacKey)); } if (fileKey == null) { FileKey = AesStatic.GenerateKey(); } else if (FileKey.Length != 32) { throw new ArgumentOutOfRangeException(nameof(fileKey)); } FileEncryption = ContentAlgorithm.Aes256Cbc; // The file needs no HMAC as we have an SHA256 } }
private async void BtnSendFile_Click(object sender, EventArgs e) { string path; using (OpenFileDialog fd = new OpenFileDialog()) { fd.InitialDirectory = Program.TempPath; if (fd.ShowDialog() == DialogResult.Cancel) { return; } path = fd.FileName; } ContentAlgorithm algorithm = ContentAlgorithm.None; byte[] aesKey = null; byte[] hmacKey = null; if (!string.IsNullOrWhiteSpace(TbFileKey.Text)) { algorithm = ContentAlgorithm.Aes256CbcHmacSha256; byte[] keys = Util.GetBytes(TbFileKey.Text); hmacKey = keys.Take(32); aesKey = keys.Skip(32); } FTEventArgs args = new FTEventArgs(new Identifier(0), await FileMeta.FromFileAsync(path, algorithm, hmacKey, aesKey, null), path); args.Progress += VslClient_FTProgress; args.Finished += VslClient_FTFinished; if (!await vslClient.FileTransfer.StartUploadAsync(args)) { return; } btnReceiveFile.Enabled = false; btnSendFile.Enabled = false; }
/// <summary> /// Initializes a new instance of the <see cref="FileMeta"/> class and generates all required keys. /// </summary> /// <param name="path">The local file path to load the meta data from.</param> /// <param name="algorithm">The cryptographic algorith to encrypt this <see cref="FileMeta"/>.</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="FileNotFoundException"/> /// <exception cref="NotSupportedException"/> public static Task <FileMeta> FromFileAsync(string path, ContentAlgorithm algorithm) { return(FromFileAsync(path, algorithm, null, null, null)); }