public static string ToHashString(HashRecord hr) { string strHash; string ret; strHash = HexConverter.ToHexString(hr.hash, HexConverter.OPTION.Lower); switch (hr.hformat) { case HashFormat.Default: ret = strHash + " " + hr.file; break; case HashFormat.BSD: switch (hr.htype) { case HashType.MD5: ret = "MD5 (" + hr.file + ") = " + strHash; break; case HashType.SHA1: ret = "SHA1 (" + hr.file + ") = " + strHash; break; case HashType.SHA256: ret = "SHA256 (" + hr.file + ") = " + strHash; break; case HashType.SHA384: ret = "SHA384 (" + hr.file + ") = " + strHash; break; case HashType.SHA512: ret = "SHA512 (" + hr.file + ") = " + strHash; break; default: throw new HashException("invalid hash algorithm"); } break; default: throw new HashException("invalid hash format"); } return(ret); }
public static bool HashEquals(HashRecord hash1, HashRecord hash2) { if (hash1.hash.Length != hash2.hash.Length) { return(false); } for (int i = 0; i < hash1.hash.Length; i++) { if (hash1.hash [i] != hash2.hash [i]) { return(false); } } return(true); }
public static HashRecord GetHashRecordFromString(string line) { string file = null; byte[] hash = null; HashFormat format = HashFormat.Invalid; HashType htype = HashType.Invalid; HashRecord hr = null; string tmp = null; format = GetHashFormatFromString(line); if (format == HashFormat.Default) { htype = GetHashTypeFromString(line, format); if (htype == HashType.Invalid) { return(null); } tmp = line.Substring(0, line.IndexOf(" ")); file = line.Substring(line.IndexOf(" ") + 2); } else if (format == HashFormat.BSD) { htype = GetHashTypeFromString(line, format); if (htype == HashType.Invalid) { return(null); } tmp = line.Substring(line.LastIndexOf(" = ") + 3); file = line.Substring(line.IndexOf('(') + 1, line.LastIndexOf(')') - line.IndexOf('(') - 1); } else { return(null); } hash = HexConverter.ToByteArray(tmp); hr = new HashRecord(file, hash, htype, format); return(hr); }