public static byte[] HashPasswordArgon2(byte[] password, byte[] salt, byte[] associatedData = null, byte[] knownSecret = null, Argon2Algorithm algorithm = Argon2Algorithm.i, int degreeOfParallelism = 2, int memorySize = 5120, int iterations = 10, int resultLength = 256) { Argon2 argon2; switch (algorithm) { case Argon2Algorithm.i: argon2 = new Argon2i(password); break; case Argon2Algorithm.d: argon2 = new Argon2d(password); break; case Argon2Algorithm.id: default: throw new NotSupportedException(); } argon2.DegreeOfParallelism = degreeOfParallelism; argon2.MemorySize = memorySize; argon2.Iterations = iterations; argon2.Salt = salt; argon2.AssociatedData = associatedData; argon2.KnownSecret = knownSecret; var hash = argon2.GetBytes(resultLength); argon2.Dispose(); return(hash); }
/// <summary> /// 指定されたパスワード、元のファイル、出力先で暗号化及び復号を非同期的に行います /// </summary> /// <param name="password"></param> /// <param name="outfile"></param> /// <returns></returns> public virtual async Task WriteStreamAsync(string password, string readPath, string outPath, CancellationToken token) { try { switch (CryptoMode) { case CryptoMode.ENCRYPTION: //暗号化 await FileEncryption(password, readPath, outPath + @"\" + Path.GetFileName(readPath) + ".safer", token); break; case CryptoMode.DENCRYPTION: //復号 await FileDecryption(password, readPath, outPath + @"\" + Path.GetFileName(readPath).Replace(".safer", ""), token); break; } } catch (OperationCanceledException) { MessageBox.Show("操作を中止しました。", "確認", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (CryptographicException) { if (token.IsCancellationRequested) { MessageBox.Show("操作を中止しました。", "確認", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } MessageBox.Show("パスワードが間違っていませんか?", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { rijndael.Dispose(); argon2?.Dispose(); } }