public ChecksumSet(string fileName, string directory, ChecksumType setType) { VerificationFileName = fileName; Directory = directory; Type = setType; Files = new List <ChecksumFile>(); }
// TODO: Localize "Line" public InvalidChecksumFileException(string fileName, ChecksumType checksumType, string[] lines, int lineIndex, int codePage) : base(string.Format("Line {0}: \"{1}\"", lineIndex + 1, lines[lineIndex]) + Environment.NewLine + Environment.NewLine + string.Format(Language.MainForm.FileContentsNotRecognized, fileName, checksumType, File.ReadAllText(fileName, Encoding.GetEncoding(codePage))) ) { _fileName = fileName; }
public static void RegisterChecksumAlgorithm( ChecksumType type, ChecksumConstructor checksumFunc ) { RegisterChecksumAlgorithm(type, checksumFunc, isWeakAlgorithm: false); }
public ChecksumSet(string fileName, string directory, ChecksumType setType) { VerificationFileName = fileName; Directory = directory; Type = setType; Files = new List<ChecksumFile>(); }
private void SendTgsRequest(string sName, KdcOptions kdcOptions, Asn1SequenceOf <PA_DATA> seqPadata = null, AuthorizationData dataInAuthentiator = null, AuthorizationData dataInEncAuthData = null, MsgType msgType = MsgType.KRB_TGS_REQ) { if (string.IsNullOrEmpty(sName)) { throw new ArgumentNullException("sName"); } PrincipalName sname = new PrincipalName(new KerbInt32((int)PrincipalType.NT_SRV_INST), KerberosUtility.String2SeqKerbString(sName.Split('/'))); KDC_REQ_BODY kdcReqBody = this.CreateKdcRequestBody(kdcOptions, sname, dataInEncAuthData); // almost same as AS request Asn1BerEncodingBuffer bodyBuffer = new Asn1BerEncodingBuffer(); kdcReqBody.BerEncode(bodyBuffer); ChecksumType checksumType = KerberosUtility.GetChecksumType(this.Context.SelectedEType); PA_DATA paTgsReq = CreatePaTgsReqest(checksumType, bodyBuffer.Data, dataInAuthentiator); // use AS session key encrypt authenticator. Asn1SequenceOf <PA_DATA> tempPaData = null; if (seqPadata == null || seqPadata.Elements == null || seqPadata.Elements.Length == 0) { tempPaData = new Asn1SequenceOf <PA_DATA>(new PA_DATA[] { paTgsReq }); } else { PA_DATA[] paDatas = new PA_DATA[seqPadata.Elements.Length + 1]; Array.Copy(seqPadata.Elements, paDatas, seqPadata.Elements.Length); paDatas[seqPadata.Elements.Length] = paTgsReq; tempPaData = new Asn1SequenceOf <PA_DATA>(paDatas); } KerberosTgsRequest tgsRequest = new KerberosTgsRequest(KerberosConstValue.KERBEROSV5, kdcReqBody, tempPaData, Context.TransportType); tgsRequest.Request.msg_type.Value = (long)msgType; this.client.SendPdu(tgsRequest); }
/// <summary> /// Generate checksum supported by MS-KILE /// </summary> /// <param name="key">the key</param> /// <param name="input">input data</param> /// <param name="usage">key usage number</param> /// <param name="checksumType">checksum type</param> /// <returns>the caculated checksum</returns> /// <exception cref="ArgumentException">thrown if the checksum type is not supported</exception> public static byte[] GetChecksum( byte[] key, byte[] input, int usage, ChecksumType checksumType) { switch (checksumType) { case ChecksumType.CRC32: case ChecksumType.rsa_md4: case ChecksumType.rsa_md5: case ChecksumType.sha1: return(UnkeyedChecksum.GetMic(input, checksumType)); case ChecksumType.hmac_sha1_96_aes128: return(HmacSha1AesChecksum.GetMic( key, input, usage, AesKeyType.Aes128BitsKey)); case ChecksumType.hmac_sha1_96_aes256: return(HmacSha1AesChecksum.GetMic( key, input, usage, AesKeyType.Aes256BitsKey)); case ChecksumType.hmac_md5_string: return(HmacMd5StringChecksum.GetMic(key, input, usage)); default: throw new ArgumentException("Unsupported checksum type."); } }
public Checksum(Stream stream, ChecksumType type = ChecksumType.MD5) { Type = type; var chksum = string.Empty; using (ICheckSumCalculator checkSumCalc = new ChecksumCalculator(stream)) { switch (Type) { case ChecksumType.MD5: chksum = checkSumCalc.CalculateMD5(); break; case ChecksumType.SHA1: chksum = checkSumCalc.CalculateSHA1(); break; case ChecksumType.SHA256: chksum = checkSumCalc.CalculateSHA256(); break; case ChecksumType.SHA384: chksum = checkSumCalc.CalculateSHA384(); break; case ChecksumType.SHA512: chksum = checkSumCalc.CalculateSHA512(); break; } } Value = chksum; }
public static void RegisterChecksumAlgorithm( ChecksumType type, ChecksumConstructor checksumFunc ) { ChecksumAlgorithms[type] = checksumFunc; }
public KileApRequest CreateApRequest( ApOptions apOptions, ChecksumType checksumType, int seqNumber, ChecksumFlags flag, EncryptionKey subkey, AuthorizationData authorizationData) { var request = new KileApRequest(context); request.Authenticator = CreateAuthenticator( checksumType, seqNumber, flag, subkey, authorizationData, context.ApSessionKey, null); request.Request.ap_options = new APOptions(KerberosUtility.ConvertInt2Flags((int)apOptions)); request.Request.msg_type = new Asn1Integer((int)MsgType.KRB_AP_REQ); request.Request.pvno = new Asn1Integer(ConstValue.KERBEROSV5); request.Request.ticket = context.ApTicket; return(request); }
public static string GetFileChecksum(string fileName, ChecksumType checksumType) { HashAlgorithm hasher = null; string hash = String.Empty; try { switch (checksumType) { case ChecksumType.MD5: hasher = MD5.Create(); break; case ChecksumType.SHA1: hasher = SHA1.Create(); break; case ChecksumType.SHA256: hasher = SHA256.Create(); break; case ChecksumType.SHA512: hasher = SHA512.Create(); break; } try { // compute hash from file using (StreamReader fileReader = new StreamReader(fileName)) { hash = ByteHashToString(hasher.ComputeHash(fileReader.BaseStream)); } } catch (IOException) { // try again with long path using (StreamReader fileReader = new StreamReader(@"\\?\" + fileName)) { hash = ByteHashToString(hasher.ComputeHash(fileReader.BaseStream)); } } } catch (Exception) { hash = String.Empty; } if (hasher != null) { // Clear() "Releases all resources used by the HashAlgorithm class" hasher.Clear(); // NOTE: HashAlgorithm.Dispose is private, so let's cast to IDisposable anyways and it'll work ((IDisposable)hasher).Dispose(); } return(hash); }
public static Packet GeneratePacket(List <Frame> frames, ChecksumType checksumType, int checksumLength) { IChecksumCalculator calculator = ChecksumCalculatorFactory.Create(checksumType); string framesData = string.Join("", frames.Select(f => f.GetFrame())); string checksum = calculator.Calculate(framesData, checksumLength); return(new Packet(frames, checksum)); }
/// <summary> /// Initializes a new instance of the FileChecksum class, and loads the file's hash (computed immediately) /// </summary> /// <param name="fileName">File to compute hash of</param> /// <param name="basePath">Base path</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> public FileChecksum(string fileName, string basePath, PathType pathType, ChecksumType checksumType) { FilePath = fileName; ResolvedFileName = GetFileName(FilePath, basePath, pathType); Checksum = FileUtils.GetFileChecksum(fileName, checksumType); }
/// <summary> /// Initializes a new instance of the FileChecksum class, and loads the file's hash (computed immediately) /// </summary> /// <param name="fileName">File to compute hash of</param> /// <param name="basePath">Base path</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> public FileChecksum(string fileName, string basePath, PathType pathType, ChecksumType checksumType) { FilePath = fileName; ResolvedFileName = GetFileName(FilePath, basePath, pathType); Checksum = FileUtils.GetFileChecksum(fileName, checksumType); }
public Message(byte frameId, Direction direction, ChecksumType checksumType, byte[] data, byte length) { FrameId = frameId; Direction = direction; ChecksumType = checksumType; Data = data; Length = length; Checksum = 0; }
public static Frame GenerateFrame(int length, ChecksumType checksumType, int checksumLength) { string data = GenerateBytes(length); IChecksumCalculator calculator = ChecksumCalculatorFactory.Create(checksumType); string checksum = calculator.Calculate(data, checksumLength); return(new Frame(data, checksum)); }
private static byte[] SetSignatureValue(ChecksumType type, Func <int, byte[]> setterFunc) { return(type switch { ChecksumType.KERB_CHECKSUM_HMAC_MD5 => setterFunc(16), ChecksumType.HMAC_SHA1_96_AES128 => setterFunc(12), ChecksumType.HMAC_SHA1_96_AES256 => setterFunc(12), _ => throw new InvalidOperationException($"Unknown checksum type {type}"), });
private PA_DATA CreatePaTgsReqest(ChecksumType checksumType, byte[] checksumBody, AuthorizationData data) { APOptions option = new APOptions(KerberosUtility.ConvertInt2Flags((int)ApOptions.None)); EncryptionKey key = Context.SessionKey; KerberosApRequest apRequest = CreateApRequest(option, Context.Ticket, null, data, KeyUsageNumber.TG_REQ_PA_TGS_REQ_padataOR_AP_REQ_Authenticator, checksumType, checksumBody); PaTgsReq paTgsReq = new PaTgsReq(apRequest.Request); return(paTgsReq.Data); }
public KileApRequest CreateApRequest(ApOptions apOptions, ChecksumType checksumType, int seqNumber, ChecksumFlags flag, EncryptionKey subkey, AuthorizationData authorizationData) { return(CreateApRequest(context.UserRealm, context.UserName, apOptions, checksumType, seqNumber, flag, subkey, authorizationData)); }
/// <summary> /// Creates a new <see cref="FormatDescriptionEvent"/>. /// </summary> public FormatDescriptionEvent( EventHeader header, int binlogVersion, string serverVersion, ChecksumType checksumType) : base(header) { BinlogVersion = binlogVersion; ServerVersion = serverVersion; ChecksumType = checksumType; }
public static List <ChecksumFile> GetCache(ChecksumType checksumType, string baseDirectory) { if (!Program.Settings.General.UseCachedResults) { return(new List <ChecksumFile>()); } string getCacheSql; if (checksumType == ChecksumType.MD5) { getCacheSql = "select FileName, LastWriteUtc, FileLength, MD5 as Checksum from MD5FileCache where FileName like @Directory + '%'"; } else if (checksumType == ChecksumType.SFV) { getCacheSql = "select FileName, LastWriteUtc, FileLength, CRC32 as Checksum from CRC32FileCache where FileName like @Directory + '%'"; } else if (checksumType == ChecksumType.SHA1) { getCacheSql = "select FileName, LastWriteUtc, FileLength, SHA1 as Checksum from SHA1FileCache where FileName like @Directory + '%'"; } else { throw new Exception(string.Format("{0} not implemented", checksumType)); } DataSet ds = new DataSet(); using (SqlCeCommand cmd = new SqlCeCommand(getCacheSql, Program.GetOpenCacheConnection())) using (SqlCeDataAdapter da = new SqlCeDataAdapter(cmd)) { SqlCeParameter param = new SqlCeParameter("@Directory", SqlDbType.NVarChar); param.Value = baseDirectory; cmd.Parameters.Add(param); da.Fill(ds); } List <ChecksumFile> files = new List <ChecksumFile>(); foreach (DataRow dr in ds.Tables[0].Rows) { ChecksumFile file = new ChecksumFile(null); file.FileName = Convert.ToString(dr["FileName"]); file.CacheLastWriteUtc = Convert.ToDateTime(dr["LastWriteUtc"]); file.CacheLength = Convert.ToInt64(dr["FileLength"]); file.OriginalChecksum = Convert.ToString(dr["Checksum"]); files.Add(file); } return(files); }
private void RunHash(string filePath, ChecksumType type) { this.lblResult.Text = ""; this.txtSource.Text = filePath; Checker ch = new Checker(); this.txtChecksum.Text = ch.GetChecksum(filePath, type); if (txtCompare.Text.Length > 0) lblResult.Text = CompareHashes(txtChecksum.Text, txtCompare.Text); }
private static byte[] SetSignatureValue(ChecksumType type, Func <int, byte[]> setterFunc) { var checksum = CryptoService.CreateChecksum(type); if (checksum == null) { throw new InvalidOperationException($"Unknown checksum type {type}"); } return(setterFunc(checksum.ChecksumSize)); }
/// <summary> /// Resets the CRC-16 data checksum as if no update was ever called. /// </summary> /// <param name="checksumType">Type of CRC calculation. CRC-16 resets to 0x0000, ModBus resets to 0xFFFF</param> public void Reset(ChecksumType checksumType) { if (checksumType == ChecksumType.ModBus) { crc = ModBusSeed; } else { crc = Crc16Seed; } }
/// <summary> /// /// </summary> /// <param name="checksumType"></param> protected override void FixChecksumValues(ChecksumType checksumType) { // Loop through all chunks and fix the checksum in the INI2 and INI3 (in OS update 1.5/1.6) chunk. // LV: Add "WSQ1" and "DKT1" if WaveSequenceBanks and DrumKits become editable? List <string> checksumChunks = new List <string> { "PBK1", "MBK1", "CBK1", "SBK1", "GLB1", "WBK1", "DBK1" }; int pbkIndex = 0; int mbkIndex = 0; int cbkIndex = 0; int wsqIndex = 0; int dbkIndex = 0; int startIndex = checksumType == ChecksumType.Kronos1516 ? 3 : 0; // For OS1.5/1.6, skip DIV1 and INI2/3. for (int index = startIndex; index < Chunks.Collection.Count; index++) { IChunk chunk = Chunks.Collection[index]; if (checksumChunks.Contains(chunk.Name)) { int checksum = 0; for (int dataIndex = chunk.Offset + 12; dataIndex < chunk.Offset + chunk.Size + 12; dataIndex++) { // Since checksum is a byte it will be automatically moduloed by 256. checksum = (checksum + Content[dataIndex]) % 256; } if (checksumType == ChecksumType.Kronos1516) { // Save in INI2. int offsetInIni2; // offsetInIni2 = FindIni2Or3Offset(chunk.Name, 0); // IMPR: wrong checksum calculated if (FindIni2Offset(chunk, out offsetInIni2, ref pbkIndex, ref mbkIndex, ref cbkIndex, ref wsqIndex, ref dbkIndex)) { continue; } Debug.Assert(offsetInIni2 >= 4); // Don't overwrite KORG header Content[offsetInIni2 + 54] = (byte)checksum; //Console.WriteLine(string.Format( // "Chunk {0} offset {1:x} size {2:x} has checksum ({3:x}..{4:x}): {5:x}, written to {6:x} and {7:x}", // chunk.Name, chunk.Offset, chunk.Size, // chunk.Offset + 12, chunk.Offset + chunk.Size + 12, checksum, offsetInIni2 + 54, // chunk.Offset + 11)); } Debug.Assert(chunk.Offset >= 4); // Don't overwrite KORG header Content[chunk.Offset + 11] = (byte)checksum; // 11 is checksum byte offset } } }
/// <summary> /// /// </summary> /// <param name="checksumType"></param> protected override void FixChecksumValues(ChecksumType checksumType) { // Loop through all chunks and fix the checksum. List <string> checksumChunks = new List <string> { "PBK1", "MBK1", "CBK1", "SBK1", "GLB1" }; int mbkIndex = 0; int cbkIndex = 0; Chunks.Collection.Where(chunk => checksumChunks.Contains(chunk.Name)).Aggregate( 0, (current, chunk) => FixChecksumValue(chunk, current, ref mbkIndex, ref cbkIndex)); }
public static void RegisterChecksumAlgorithm( ChecksumType type, ChecksumConstructor checksumFunc, bool isWeakAlgorithm ) { ChecksumAlgorithms[type] = checksumFunc; if (isWeakAlgorithm) { WeakChecksumTypes.Add(type); } }
/// <summary> /// Calculates the file's checksum and adds it to the Database /// </summary> /// <param name="fileName">File to add</param> /// <param name="basePath">Base path</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> /// <returns>Checksum of the file</returns> public string AddFile(string fileName, string basePath, PathType pathType, ChecksumType checksumType) { _hasChanges = true; // load file checksum FileChecksum fc = new FileChecksum(fileName, basePath, pathType, checksumType); // add to database _fileLookup.Add(fc.ResolvedFileName, fc); // return the checksum return(fc.Checksum); }
internal static KerberosChecksum CreateChecksum( ChecksumType type, ReadOnlyMemory <byte> signature = default, ReadOnlyMemory <byte> signatureData = default ) { if (ChecksumAlgorithms.TryGetValue(type, out ChecksumConstructor func) && func != null) { return(func(signature, signatureData)); } return(null); }
public FileValidation(byte[] data, ChecksumType type = ChecksumType.Murmur128) { if (data == null) { throw new ArgumentNullException("data"); } SetContents(() => { using (var hash = GetHash()) return(hash.ComputeHash(data)); }, (uint)data.LongLength, type); }
public string GetChecksum(string filePath, ChecksumType type) { if(!File.Exists(filePath)) return ""; string hash = ""; using(var fs = new FileStream(filePath, FileMode.Open)) { using (var cryptoProvider = GetCryptoProvider(type)) { hash = BitConverter.ToString(cryptoProvider.ComputeHash(fs)); } } return hash; }
public FileValidation(Stream stream, ChecksumType type = ChecksumType.Murmur128) { if (stream == null) { throw new ArgumentNullException("stream"); } _stream = stream; SetContents(() => { using (stream) using (var hash = GetHash()) return(hash.ComputeHash(stream)); }, (uint)stream.Length, type); }
public static string GetFileChecksum(string fileName, ChecksumType checksumType) { HashAlgorithm hasher = null; string hash = String.Empty; try { switch (checksumType) { case ChecksumType.MD5: hasher = MD5.Create(); break; case ChecksumType.SHA1: hasher = SHA1.Create(); break; case ChecksumType.SHA256: hasher = SHA256.Create(); break; case ChecksumType.SHA512: hasher = SHA512.Create(); break; } // compute hash from file using (StreamReader fileReader = new StreamReader(fileName)) { hash = ByteHashToString(hasher.ComputeHash(fileReader.BaseStream)); } } catch (IOException) { hash = String.Empty; } if (hasher != null) { // Clear() "Releases all resources used by the HashAlgorithm class" hasher.Clear(); // NOTE: HashAlgorithm.Dispose is private, so let's cast to IDisposable anyways and it'll work ((IDisposable)hasher).Dispose(); } return hash; }
/// <summary> /// Create authenticator for AP request or part of PA-DATA for TGS request. /// </summary> /// <param name="cRealm">This field contains the name of the realm in which the client is registered and in /// which initial authentication took place.</param> /// <param name="cName">This field contains the name part of the client's principal identifier.</param> /// <param name="checksumType">The checksum type selected.</param> /// <param name="seqNumber">The current local sequence number.</param> /// <param name="flag">The flag set in checksum field of Authenticator.</param> /// <param name="subkey">Specify the new subkey used in the following exchange. This field is optional. /// This argument can be got with method GenerateKey(ApSessionKey). /// This argument can be null. If this argument is null, no subkey will be sent.</param> /// <param name="authorizationData">The authentication data of authenticator. This field is optional. /// This argument can be generated by method ConstructAuthorizationData. This argument can be null. /// If this argument is null, no Authorization Data will be sent.</param> /// <param name="key">The key to do checksum.</param> /// <param name="checksumBody">The data to compute checksum.</param> /// <returns>The created authenticator.</returns> private Authenticator CreateAuthenticator(Realm cRealm, PrincipalName cName, ChecksumType checksumType, int seqNumber, ChecksumFlags flag, EncryptionKey subkey, AuthorizationData authorizationData, EncryptionKey key, byte[] checksumBody) { Authenticator plaintextAuthenticator = new Authenticator(); plaintextAuthenticator.authenticator_vno = new Asn1Integer(ConstValue.KERBEROSV5); plaintextAuthenticator.crealm = cRealm; plaintextAuthenticator.cname = cName; plaintextAuthenticator.cusec = new Microseconds(0); plaintextAuthenticator.ctime = KileUtility.CurrentKerberosTime; plaintextAuthenticator.seq_number = new KerbUInt32(seqNumber); plaintextAuthenticator.subkey = subkey; plaintextAuthenticator.authorization_data = authorizationData; if (checksumType == ChecksumType.ap_authenticator_8003) { // compute the checksum AuthCheckSum checksum = new AuthCheckSum(); checksum.Lgth = ConstValue.AUTHENTICATOR_CHECKSUM_LENGTH; checksum.Bnd = new byte[checksum.Lgth]; checksum.Flags = (int)flag; byte[] checkData = ArrayUtility.ConcatenateArrays(BitConverter.GetBytes(checksum.Lgth), checksum.Bnd, BitConverter.GetBytes(checksum.Flags)); // in AP request plaintextAuthenticator.cksum = new Checksum(new KerbInt32((int)checksumType), new Asn1OctetString(checkData)); } else { // in TGS PA data byte[] checkData = KileUtility.GetChecksum( key.keyvalue.ByteArrayValue, checksumBody, (int)KeyUsageNumber.TGS_REQ_PA_TGS_REQ_adataOR_AP_REQ_Authenticator_cksum, checksumType); plaintextAuthenticator.cksum = new Checksum(new KerbInt32((int)checksumType), new Asn1OctetString(checkData)); } return(plaintextAuthenticator); }
private static byte[] SetSignatureValue(ChecksumType type, Func <int, byte[]> setterFunc) { switch (type) { case ChecksumType.KERB_CHECKSUM_HMAC_MD5: return(setterFunc(16)); case ChecksumType.HMAC_SHA1_96_AES128: return(setterFunc(12)); case ChecksumType.HMAC_SHA1_96_AES256: return(setterFunc(12)); } throw new InvalidOperationException($"Unknown checksum type {type}"); }
internal static KerberosChecksum CreateChecksumValidator(ChecksumType type, ReadOnlyMemory <byte> signature, ReadOnlyMemory <byte> signatureData) { switch (type) { case ChecksumType.KERB_CHECKSUM_HMAC_MD5: return(new HmacMd5KerberosChecksum(signature, signatureData)); case ChecksumType.HMAC_SHA1_96_AES128: return(new HmacAes128KerberosChecksum(signature, signatureData)); case ChecksumType.HMAC_SHA1_96_AES256: return(new HmacAes256PacSign(signature, signatureData)); } return(null); }
private HashAlgorithm GetCryptoProvider(ChecksumType type) { switch (type) { case ChecksumType.SHA1: return new SHA1Managed(); case ChecksumType.SHA256: return new SHA256Managed(); case ChecksumType.MD5: return new MD5CryptoServiceProvider(); default: throw new ApplicationException("Unsupported Checksum type"); } }
public static IChecksumCalculator Create(ChecksumType checksumType) { if (checksumType == ChecksumType.ParityBit) { return(new ParityBitCalculator()); } if (checksumType == ChecksumType.SumOfBytes) { return(new ByteSumCalculator()); } if (checksumType == ChecksumType.Crc) { return(new CrcCalculator()); } throw new NotSupportedException($"Checksum of type {checksumType} is not supported yet."); }
/// <summary> /// Creates an engine of the specified directory under corpus\. /// <para /> /// The database .xml file should be in corpus\, named the same as the subdirectory. /// </summary> /// <param name="subdirectory">Subdirectory of corpus\ to use</param> /// <param name="excludePattern">Exclude pattern</param> /// <param name="matchPattern">Match pattern</param> /// <param name="matchType">Match type</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> /// <returns>Checksum Engine</returns> protected static Engine CreateEngine( string subdirectory, string excludePattern = "", string matchPattern = "*", MatchType matchType = MatchType.Wildcard, PathType pathType = PathType.RelativePath, ChecksumType checksumType = ChecksumType.MD5) { Engine engine = new Engine( GetCorpusDatabase(subdirectory), new NullReporter(), GetCorpusDirectory(subdirectory), excludePattern, matchPattern, matchType, pathType, checksumType); return engine; }
/// <summary> /// Calculate a unkeyed checksum (CRC32, rsa_md4, rsa_md5, sha1) /// </summary> /// <param name="input">input data</param> /// <param name="checksumType">checksum type</param> /// <returns>the calculated checksum</returns> public static byte[] GetMic(byte[] input, ChecksumType checksumType) { switch (checksumType) { case ChecksumType.CRC32: return CryptoUtility.ComputeCRC32(input); case ChecksumType.rsa_md4: return CryptoUtility.ComputeMd4(input); case ChecksumType.rsa_md5: return CryptoUtility.ComputeMd5(input); case ChecksumType.sha1: return CryptoUtility.ComputeSha1(input); default: throw new ArgumentException( "Not a valid unkeyed checksum type."); } }
public static string ComputeChecksum(FileStream file, ChecksumType.Type type) { switch (type) { case ChecksumType.Type.MD5: return Convert.ToBase64String(MD5.Create().ComputeHash(file)); case ChecksumType.Type.SHA_256: return Convert.ToBase64String(SHA256.Create().ComputeHash(file)); case ChecksumType.Type.SHA_512: return Convert.ToBase64String(SHA512.Create().ComputeHash(file)); case ChecksumType.Type.CRC_32: return Convert.ToBase64String(Crc32.Create().ComputeHash(file)); case ChecksumType.Type.CRC_32C: return Convert.ToBase64String(Crc32C.Create().ComputeHash(file)); default: return ""; } }
public static List<ChecksumFile> GetCache(ChecksumType checksumType, string baseDirectory) { if (!Program.Settings.General.UseCachedResults) return new List<ChecksumFile>(); string getCacheSql; if (checksumType == ChecksumType.MD5) getCacheSql = "select FileName, LastWriteUtc, FileLength, MD5 as Checksum from MD5FileCache where FileName like @Directory + '%'"; else if (checksumType == ChecksumType.SFV) getCacheSql = "select FileName, LastWriteUtc, FileLength, CRC32 as Checksum from CRC32FileCache where FileName like @Directory + '%'"; else if (checksumType == ChecksumType.SHA1) getCacheSql = "select FileName, LastWriteUtc, FileLength, SHA1 as Checksum from SHA1FileCache where FileName like @Directory + '%'"; else throw new Exception(string.Format("{0} not implemented", checksumType)); DataSet ds = new DataSet(); using (SqlCeCommand cmd = new SqlCeCommand(getCacheSql, Program.GetOpenCacheConnection())) using (SqlCeDataAdapter da = new SqlCeDataAdapter(cmd)) { SqlCeParameter param = new SqlCeParameter("@Directory", SqlDbType.NVarChar); param.Value = baseDirectory; cmd.Parameters.Add(param); da.Fill(ds); } List<ChecksumFile> files = new List<ChecksumFile>(); foreach (DataRow dr in ds.Tables[0].Rows) { ChecksumFile file = new ChecksumFile(null); file.FileName = Convert.ToString(dr["FileName"]); file.CacheLastWriteUtc = Convert.ToDateTime(dr["LastWriteUtc"]); file.CacheLength = Convert.ToInt64(dr["FileLength"]); file.OriginalChecksum = Convert.ToString(dr["Checksum"]); files.Add(file); } return files; }
/// <summary> /// Creates a new instance of the Crc16 class. /// </summary> /// <param name="checksumType"> /// Type of calculation to perform, CRC-16 or ModBus. /// </param> public Crc16(ChecksumType checksumType) { Reset(checksumType); }
public PutObjectRequest WithChecksum(ChecksumType checksum, ChecksumType.Type ctype = ChecksumType.Type.MD5) { this._checksum = checksum; this._ctype = ctype; return this; }
private static string ComputeChecksum(ChecksumType checksum, Stream content, ChecksumType.Type type = ChecksumType.Type.MD5) { return checksum.Match( () => "", () => { switch (type) { case ChecksumType.Type.MD5: return Convert.ToBase64String(System.Security.Cryptography.MD5.Create().ComputeHash(content)); case ChecksumType.Type.SHA_256: return Convert.ToBase64String(System.Security.Cryptography.SHA256.Create().ComputeHash(content)); case ChecksumType.Type.SHA_512: return Convert.ToBase64String(System.Security.Cryptography.SHA512.Create().ComputeHash(content)); case ChecksumType.Type.CRC_32: return Convert.ToBase64String(Ds3.Models.Crc32.Create().ComputeHash(content)); case ChecksumType.Type.CRC_32C: return Convert.ToBase64String(Ds3.Models.Crc32C.Create().ComputeHash(content)); default: return ""; } }, hash => Convert.ToBase64String(hash) ); }
/// <summary> /// Resets the CRC-16 data checksum as if no update was ever called. /// </summary> /// <param name="checksumType">Type of CRC calculation. CRC-16 resets to 0x0000, ModBus resets to 0xFFFF</param> public void Reset(ChecksumType checksumType) { if (checksumType == ChecksumType.ModBus) crc = ModBusSeed; else crc = Crc16Seed; }
public MainForm(string[] args) { InitializeComponent(); Language.Changed += delegate { SetLanguage(); }; SetLanguage(); //miFindRenamedFiles.Visible = false; //miTruncateFileNames.Visible = false; //toolStripSeparator6.Visible = false; _fsw = new FileSystemWatcher(Program.AppDataPath, "*.add"); _fsw.Created += _fsw_Created; _fsw.EnableRaisingEvents = true; notifyIcon1.BalloonTipClicked += notifyIcon1_BalloonTipClicked; Version version = Assembly.GetExecutingAssembly().GetName().Version; int major = version.Major; int minor = version.Minor; int build = version.Build; Text = string.Format("ilSFV {0}.{1}", major, minor, build); ResizeStatusBar(); chkHideGood.Checked = Program.Settings.General.HideGoodFiles; miHideGood.Checked = Program.Settings.General.HideGoodFiles; miUseCachedResults.Checked = Program.Settings.General.UseCachedResults; miFindRenamedFiles.Enabled = false; btnPause.Enabled = false; btnHide.Enabled = false; if (Program.Settings.General.RememberWindowPlacement) { if (Program.Settings.General.WindowTop == 0 && Program.Settings.General.WindowLeft == 0) { StartPosition = FormStartPosition.CenterScreen; } else { StartPosition = FormStartPosition.Manual; Top = Program.Settings.General.WindowTop; Left = Program.Settings.General.WindowLeft; } Height = Program.Settings.General.WindowHeight; Width = Program.Settings.General.WindowWidth; WindowState = Program.Settings.General.FormWindowState; } ToggleCommentsPane(); if (args != null && args.Length > 0) { int argStart = 0; switch (args[0].ToLower()) { case "/verify": _initialVerify = true; argStart++; break; case "/create": _initialCreate = true; argStart += 2; break; default: _initialVerify = true; break; } List<string> files = new List<string>(); for (int i = argStart; i < args.Length; i++) { files.Add(args[i]); } if (_initialVerify) { EngageDropTimer(files); } else if (_initialCreate) { switch (args[1].ToLower()) { case "sfv": _initialCreateChecksumType = ChecksumType.SFV; break; case "md5": _initialCreateChecksumType = ChecksumType.MD5; break; case "sha1": _initialCreateChecksumType = ChecksumType.SHA1; break; default: throw new Exception(string.Format("{0} not implemented", args[1])); } EngageCreateTimer(files); } } _timer = new Timer(); _timer.Interval = 1; _timer.Tick += _timer_Tick; _timer.Enabled = true; Load += MainForm_Load; }
private void GetWorkset(string fileName, string directoryName, ChecksumType setType, List<string> filesAndFolders, out int overwriteCount) { SetStatusText(Language.MainForm.Status_GettingFileList); Application.DoEvents(); IDictionary<string, List<string>> files; if (filesAndFolders == null) { if (Program.Settings.General.Recursive) { files = GetFilesAllDirectories(directoryName); } else { files = new Dictionary<string, List<string>>(); files.Add(directoryName, Directory.GetFiles(directoryName, "*.*", SearchOption.TopDirectoryOnly).ToList()); } } else { files = new Dictionary<string, List<string>>(); foreach (string item in filesAndFolders) { if (Directory.Exists(item)) { if (Program.Settings.General.Recursive) { foreach (var kvp in GetFilesAllDirectories(item)) { files.Add(kvp.Key, kvp.Value); } } else { files.Add(item, Directory.GetFiles(item, "*.*", SearchOption.TopDirectoryOnly).ToList()); } } else { List<string> topFiles; if (!files.TryGetValue(directoryName, out topFiles)) { topFiles = new List<string>(); files.Add(directoryName, topFiles); } topFiles.Add(item); } } } if (Program.Settings.Create.SortFiles) { SetStatusText(Language.MainForm.Status_PreSorting); Application.DoEvents(); files = new SortedDictionary<string, List<string>>(files, StringComparer.InvariantCultureIgnoreCase); foreach (KeyValuePair<string, List<string>> kvp in new Dictionary<string, List<string>>(files)) { files[kvp.Key] = kvp.Value.OrderBy(p => p).ToList(); } } SetStatusText(Language.MainForm.Status_GettingFileInfo); Application.DoEvents(); List<string> excludeExt = new List<string>(); if (!string.IsNullOrEmpty(Program.Settings.Create.ExcludeFilesOfType)) { string[] exts = Program.Settings.Create.ExcludeFilesOfType.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string ext in exts) { string extMod = ext.ToLower(); if (extMod.StartsWith("*")) extMod = extMod.Substring(1, extMod.Length - 1); if (extMod.StartsWith(".")) { if (!excludeExt.Contains(extMod)) excludeExt.Add(extMod); } } } _sets.Clear(); ChecksumSet set = new ChecksumSet(fileName, directoryName, setType); string checksumExt = Path.GetExtension(fileName); if (!Program.Settings.Create.CreateForEachSubDir) _sets.Add(set); int trimLength = directoryName.Length; if (!directoryName.EndsWith("\\")) trimLength++; int filesProcessed = 0; int lastPercent = 0; int totalFiles = 0; foreach (KeyValuePair<string, List<string>> kvp in files) { totalFiles += kvp.Value.Count; } foreach (KeyValuePair<string, List<string>> kvp in files) { if (Program.Settings.Create.CreateForEachSubDir) { string tmpFileName; if (Program.Settings.Create.PromptForFileName) { tmpFileName = Path.Combine(kvp.Key, Path.GetFileName(fileName)); } else { tmpFileName = Path.GetFileName(kvp.Key) + checksumExt; tmpFileName = Path.Combine(kvp.Key, tmpFileName); } set = new ChecksumSet(tmpFileName, kvp.Key, setType); _sets.Add(set); trimLength = kvp.Key.Length; if (!kvp.Key.EndsWith("\\")) trimLength++; } long directoryTotalSize = 0; foreach (string file in kvp.Value) { filesProcessed++; int percent = (filesProcessed * 100 / totalFiles); if (percent / 5 > lastPercent / 5) { SetStatusText(string.Format(Language.MainForm.Status_GettingFileInfoPercentage, (percent / 5) * 5)); Application.DoEvents(); lastPercent = percent; } bool ok = true; foreach (string ext in excludeExt) { if (string.Compare(Path.GetExtension(file), ext, true) == 0) { ok = false; break; } } if (ok) { FileInfo fileInfo = TryGetNewFileInfo(file); if ((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; ChecksumFile checksumFile = new ChecksumFile(set); checksumFile.FileInfo = fileInfo; checksumFile.State = ChecksumFileState.NotProcessed; checksumFile.FileName = file.Substring(trimLength, file.Length - trimLength); set.Files.Add(checksumFile); directoryTotalSize += fileInfo.Length; } } set.TotalSize += directoryTotalSize; } overwriteCount = 0; foreach (ChecksumSet setx in _sets) { if (File.Exists(setx.VerificationFileName)) overwriteCount++; } }
/// <summary> /// Initializes a new instance of the <see cref="Engine" /> class /// </summary> /// <param name="xmlDatabasePath">XML database path</param> /// <param name="reporter">Reporter to use</param> /// <param name="basePath">Base path</param> /// <param name="excludePattern">Exclude pattern</param> /// <param name="matchPattern">Match pattern</param> /// <param name="matchType">Match type</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> public Engine( string xmlDatabasePath, IEngineReporter reporter, string basePath, string excludePattern = "", string matchPattern = "*", MatchType matchType = MatchType.Wildcard, PathType pathType = PathType.RelativePath, ChecksumType checksumType = ChecksumType.MD5) { _xmlDatabasePath = xmlDatabasePath; _reporter = reporter; _basePath = basePath; _excludePattern = excludePattern; _matchPattern = matchPattern; _matchType = matchType; _pathType = pathType; _checksumType = checksumType; _db = Database.FromFile(_xmlDatabasePath, _basePath, _pathType); }
protected void runPutWithChecksum(string bucket, string srcDirectory, string filename, string hashString = null, ChecksumType.Type checksumType = ChecksumType.Type.MD5) { _helpers.EnsureBucketExists(bucket); FileInfo fileInfo = new FileInfo(Path.Combine(srcDirectory, filename)); var ds3Obj = new Ds3Object(filename, fileInfo.Length); var ds3Objs = new List<Ds3Object>(); ds3Objs.Add(ds3Obj); // create a job var job = _helpers.StartWriteJob(bucket, ds3Objs); // instantiate a PutObjectRequest FileStream fs = File.Open(srcDirectory + Path.DirectorySeparatorChar + filename, FileMode.Open); PutObjectRequest putRequest = new PutObjectRequest(bucket, filename, fs) .WithJob(job.JobId) .WithOffset(0L); if (string.IsNullOrEmpty(hashString)) { // Compute checksum putRequest.WithChecksum(ChecksumType.Compute, checksumType); } else { // or pass in a precomputed Base64 string representation of the hash putRequest.WithChecksum(ChecksumType.Value(Convert.FromBase64String(hashString)), checksumType); } _client.PutObject(putRequest); fs.Close(); }
/// <summary> /// Calculates the file's checksum and adds it to the Database /// </summary> /// <param name="fileName">File to add</param> /// <param name="basePath">Base path</param> /// <param name="pathType">Path type</param> /// <param name="checksumType">Checksum type</param> /// <returns>Checksum of the file</returns> public string AddFile(string fileName, string basePath, PathType pathType, ChecksumType checksumType) { _hasChanges = true; // load file checksum FileChecksum fc = new FileChecksum(fileName, basePath, pathType, checksumType); // add to database _fileLookup.Add(fc.ResolvedFileName, fc); // return the checksum return fc.Checksum; }
public void TestBulkPutWithChecksum(ChecksumType checksum, ChecksumType.Type checksumType, string value) { const string bucketName = "TestBulkPutWithChecksum"; try { Client.ModifyDataPolicySpectraS3( new ModifyDataPolicySpectraS3Request(FixtureName).WithChecksumType(checksumType)); // Creates a bucket if it does not already exist. Helpers.EnsureBucketExists(bucketName); // Creates a bulk job with the server based on the files in a directory (recursively). var obj = FileHelpers.ListObjectsForDirectory(TestDirectorySrc).First(); var job = Helpers.StartWriteJob(bucketName, new List<Ds3Object> {obj}); job.WithChecksum(checksum, checksumType); // Transfer all of the files. job.Transfer(FileHelpers.BuildFilePutter(TestDirectorySrc)); /******************************/ /* Verify end-to-end checksum */ /******************************/ // Creates a bulk job with all of the objects in the bucket. job = Helpers.StartReadAllJob(bucketName); // Transfer all of the files. job.Transfer(FileHelpers.BuildFileGetter(TestDirectoryDest, "checksumTest_")); foreach (var file in Directory.GetFiles(TestDirectoryDest)) { var fileName = Path.GetFileName(file); if (!fileName.StartsWith("checksumTest_")) continue; var newFile = File.OpenRead(file); Assert.AreEqual( value, Ds3TestUtils.ComputeChecksum(newFile, checksumType) ); newFile.Close(); File.Delete(file); } Ds3TestUtils.DeleteBucket(Client, bucketName); } finally { Ds3TestUtils.DeleteBucket(Client, bucketName); //restore the default checksum type on the data policy Client.ModifyDataPolicySpectraS3( new ModifyDataPolicySpectraS3Request(FixtureName).WithChecksumType(ChecksumType.Type.MD5)); } }
private PA_DATA CreatePaTgsReq(ChecksumType checksumType, byte[] checksumBody, AuthorizationData data) { APOptions option = new APOptions(KerberosUtility.ConvertInt2Flags((int)ApOptions.None)); EncryptionKey key = Context.SessionKey; EncryptionKey subkey = null; Ticket ticket = Context.Ticket.Ticket; KerberosApRequest apRequest = CreateApRequest(option, Context.Ticket, subkey, data, KeyUsageNumber.TG_REQ_PA_TGS_REQ_padataOR_AP_REQ_Authenticator, checksumType, checksumBody); PaTgsReq paTgsReq = new PaTgsReq(apRequest.Request); return paTgsReq.Data; }
public PutDataPolicySpectraS3Request WithChecksumType(ChecksumType.Type? checksumType) { this._checksumType = checksumType; if (checksumType != null) { this.QueryParams.Add("checksum_type", checksumType.ToString()); } else { this.QueryParams.Remove("checksum_type"); } return this; }
private Authenticator CreateAuthenticator( KerberosTicket ticket, AuthorizationData data, EncryptionKey subkey, ChecksumType checksumType, byte[] checksumBody ) { Authenticator plaintextAuthenticator = CreateAuthenticator(ticket, data, subkey); byte[] checkData = KerberosUtility.GetChecksum(ticket.SessionKey.keyvalue.ByteArrayValue, checksumBody, (int)KeyUsageNumber.TGS_REQ_PA_TGS_REQ_adataOR_AP_REQ_Authenticator_cksum, checksumType); plaintextAuthenticator.cksum = new Checksum(new KerbInt32((int)checksumType), new Asn1OctetString(checkData)); return plaintextAuthenticator; }
internal void Clear() { Dirty = false; m_Type = ChecksumType.None; m_Count = m_Position = -1; m_Start = 0; m_Size = 0; m_InitialValue = m_FinalXOR = m_Polynomial = 0; m_ReverseData = m_Reflection = false; }
/// <summary> /// Creates a Data Policy with the specified checksum type and end-to-end crc requirement /// and makes it the default data policy for spectra user /// </summary> /// <param name="fixtureName"></param> /// <param name="withEndToEndCrcRequired"></param> /// <param name="checksumType"></param> /// <param name="client"></param> /// <returns></returns> public static Guid SetupDataPolicy( string fixtureName, bool withEndToEndCrcRequired, ChecksumType.Type checksumType, IDs3Client client) { PutDataPolicySpectraS3Request dataPolicyRequest = new PutDataPolicySpectraS3Request(fixtureName) .WithEndToEndCrcRequired(withEndToEndCrcRequired); if (checksumType != ChecksumType.Type.NONE) { dataPolicyRequest.WithChecksumType(checksumType); } PutDataPolicySpectraS3Response dataPolicyResponse = client.PutDataPolicySpectraS3(dataPolicyRequest); client.ModifyUserSpectraS3(new ModifyUserSpectraS3Request("spectra") .WithDefaultDataPolicyId(dataPolicyResponse.ResponsePayload.Id)); return dataPolicyResponse.ResponsePayload.Id; }
public static bool CheckArguments(string[] args) { // show usage if no command line was given if (args == null || args.Length == 0) { Usage(String.Empty); return false; } // // loop through all passed in arguments // for (int i = 0; i < args.Length; i++) { switch (args[i].ToUpperInvariant()) { // // action // case "-UPDATE": _action = ProgramAction.Update; break; case "-VERIFY": _action = ProgramAction.Verify; break; // // db // case "-DB": _xmlFileName = args[++i]; break; // // generic options // case "-MATCH": _matchPattern = args[++i]; break; case "-EXCLUDE": _excludePattern = args[++i]; break; case "-BASEPATH": _basePath = args[++i]; break; case "-R": case "-RECURSE": _recurse = true; break; // // path type // case "-RELATIVEPATH": _pathType = PathType.RelativePath; break; case "-FULLPATH": _pathType = PathType.FullPath; break; case "-FULLPATHNODRIVE": _pathType = PathType.FullPathNoDrive; break; // // checksum type // case "-MD5": _checksumType = ChecksumType.MD5; break; case "-SHA1": _checksumType = ChecksumType.SHA1; break; case "-SHA256": _checksumType = ChecksumType.SHA256; break; case "-SHA512": _checksumType = ChecksumType.SHA512; break; // // -verify options // case "-IGNOREMISSING": _verifyIgnoreMissing = true; break; case "-SHOWNEW": _verifyShowNew = true; break; case "-IGNORECHECKSUM": _verifyIgnoreChecksum = true; break; // // -update options // case "-REMOVEMISSING": _updateRemoveMissing = true; break; case "-IGNORENEW": _updateIgnoreNew = true; break; case "-PRETEND": _updatePretend = true; break; default: Usage("Unknown argument: " + args[i]); return false; } } return true; }
private KerberosApRequest CreateApRequest(APOptions option, KerberosTicket ticket, EncryptionKey subkey, AuthorizationData data, KeyUsageNumber keyUsageNumber, ChecksumType checksumType, byte[] checksumBody) { Authenticator authenticator = CreateAuthenticator(ticket, data, subkey, checksumType, checksumBody); KerberosApRequest apReq = new KerberosApRequest(Context.Pvno, option, ticket, authenticator, keyUsageNumber); return apReq; }