public Pbkdf2Result(byte[] password, byte[] salt, int iteration, byte[] key) { Password = BytesEx.FromBytes(password); Salt = BytesEx.FromBytes(salt); Iteration = iteration; Key = BytesEx.FromBytes(key); }
public static BytesEx Hash(BytesEx input, HashAlgorithm algorithm) { System.Security.Cryptography.HashAlgorithm hasher = GetAlgorithm(algorithm); byte[] hashByte = hasher.ComputeHash(input.Values); BytesEx hash = BytesEx.FromBytes(hashByte); return(hash); }
public static bool Verify(string password, byte[] salt, int iteration, byte[] key) { BytesEx passwordBytes = BytesEx.FromString(password); BytesEx saltBytes = BytesEx.FromBytes(salt); BytesEx keyBytes = BytesEx.FromBytes(key); Pbkdf2Result pbkdf2 = new Pbkdf2Result(passwordBytes, saltBytes, iteration, keyBytes); return(Verify(pbkdf2)); }
internal static FileDescriptor GetFileDescriptor(FileGetFileDescriptorArgument args) { string realPath = PathExpress.ResolvePath(args.Path); string parent = Path.GetDirectoryName(realPath); string fullName = Path.GetFileName(realPath); string name = Path.GetFileNameWithoutExtension(fullName); string extension = Path.GetExtension(fullName); FileMetadata metadata = null; BytesEx md5Hash = null; BytesEx sha1Hash = null; BytesEx sha256Hash = null; BytesEx sha512Hash = null; if (args.IncludeAttribute) { FileInfo fileInfo = new FileInfo(realPath); bool isArchive = fileInfo.Attributes.HasFlag(FileAttributes.Archive); bool isReadOnly = fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly); bool isHidden = fileInfo.Attributes.HasFlag(FileAttributes.Hidden); bool isSystem = fileInfo.Attributes.HasFlag(FileAttributes.System); long length = fileInfo.Length; DateTime createdTime = fileInfo.CreationTimeUtc; DateTime modifiedTime = fileInfo.LastWriteTimeUtc; DateTime accessTime = fileInfo.LastAccessTimeUtc; metadata = new FileMetadata(isArchive, isHidden, isReadOnly, isSystem, length, createdTime, accessTime, modifiedTime); } if (args.IncludeMd5 || args.IncludeSha1 || args.IncludeSha256 || args.IncludeSha512) { string md5 = "md5"; string sha1 = "sha1"; string sha256 = "sha256"; string sha512 = "sha512"; Dictionary <string, System.Security.Cryptography.HashAlgorithm> cryptDictionary = new Dictionary <string, System.Security.Cryptography.HashAlgorithm>(); if (args.IncludeMd5) { cryptDictionary[md5] = HashExpress.GetAlgorithm(HashAlgorithm.Md5); } if (args.IncludeSha1) { cryptDictionary[sha1] = HashExpress.GetAlgorithm(HashAlgorithm.Sha1); } if (args.IncludeSha256) { cryptDictionary[sha256] = HashExpress.GetAlgorithm(HashAlgorithm.Sha256); } if (args.IncludeSha512) { cryptDictionary[sha512] = HashExpress.GetAlgorithm(HashAlgorithm.Sha512); } System.Security.Cryptography.HashAlgorithm[] crypts = cryptDictionary.Values.ToArray(); using (Stream fileStream = new FileStream(realPath, FileMode.Open)) { byte[] buffer = new byte[HashExpress.StreamBufferSize]; int read; long currentPosition = 0L; long streamLength = fileStream.Length; while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0) { currentPosition += read; foreach (System.Security.Cryptography.HashAlgorithm crypt in crypts) { if (currentPosition < streamLength) { int _ = crypt.TransformBlock(buffer, 0, read, buffer, 0); } else { byte[] _ = crypt.TransformFinalBlock(buffer, 0, read); } } } if (args.IncludeMd5) { md5Hash = BytesEx.FromBytes(cryptDictionary[md5].Hash); } if (args.IncludeSha1) { sha1Hash = BytesEx.FromBytes(cryptDictionary[sha1].Hash); } if (args.IncludeSha256) { sha256Hash = BytesEx.FromBytes(cryptDictionary[sha256].Hash); } if (args.IncludeSha512) { sha512Hash = BytesEx.FromBytes(cryptDictionary[sha512].Hash); } } } FileDescriptor result = new FileDescriptor(name, extension, parent, metadata, md5Hash, sha1Hash, sha256Hash, sha512Hash); return(result); }