public void ObjectEqualsTest() { foreach (long highBitsX in new long[] { 0L, 0x12345678L, 0x32L }) { foreach (long lowBitsX in new long[] { 0L, 0x12345678L, 0x32L }) { object targetX = new MD5Checksum(GetByteArray(lowBitsX, highBitsX)); foreach (long highBitsY in new long[] { 0L, 0x12345678L, 0x32L }) { foreach (long lowBitsY in new long[] { 0L, 0x12345678L, 0x32L }) { object targetY = new MD5Checksum(GetByteArray(lowBitsY, highBitsY)); if (highBitsX == highBitsY && lowBitsX == lowBitsY) { Assert.True(targetX.Equals(targetY)); Assert.True(targetY.Equals(targetX)); Assert.Equal(targetX, targetY); } else { Assert.False(targetX.Equals(targetY)); Assert.False(targetY.Equals(targetX)); Assert.NotEqual(targetX, targetY); } } } } } }
/// <summary> /// Check if data matching the provided MD5 already exists /// </summary> /// <param name="MD5"></param> /// <returns></returns> public bool Exists(MD5Checksum MD5) { lock (this) { return(this.archiveIndexMD5.ContainsKey(MD5)); } }
public override void ToStream(Stream output) { output.Write(TLUtils.SignatureToBytes(Signature)); output.Write(Id.ToBytes()); output.Write(Parts.ToBytes()); output.Write(MD5Checksum.ToBytes()); output.Write(KeyFingerprint.ToBytes()); }
IFileCompareStats IFileStats.AsCompared(DateTime compared, MD5Checksum checksum, Guid groupID) { compared = FileStats.NormalizeDateTime(compared); if (compared.Equals(_md5.LastCalculatedUTC)) { return(new FileCompareStats(_md5.Length, compared, checksum, groupID)); } return(new FileCompareStatsVerifiedMD5(_md5.Length, _md5.LastCalculatedUTC, checksum, groupID, compared)); }
public FileMD5Stats(IFileStats stats, MD5Checksum checksum) { if (stats == null) { throw new ArgumentNullException("stats"); } _fileStats = (stats is FileStats) ? (FileStats)stats : new FileStats(stats.Length, stats.LengthLastVerifedUTC); _checksum = checksum; }
public IFileStats ValidateChecksum(long length, MD5Checksum checksum, DateTime lastCalculated) { lastCalculated = FileStats.NormalizeDateTime(lastCalculated); if (length == _md5.Length && checksum.Equals(_md5.Checksum)) { return((lastCalculated.Equals(_lastCompared)) ? (IFileCompareStats)(new FileCompareStats(_md5.Length, lastCalculated, checksum, _groupID)) : new FileCompareStatsVerifiedMD5(this, lastCalculated)); } return(new FileMD5Stats(_md5.Length, lastCalculated, _md5.Checksum)); }
internal FsFileData GetFile(MD5Checksum checksum, long length) { Monitor.Enter(_fileData); try { throw new NotImplementedException(); } finally { Monitor.Exit(_fileData); } }
public override byte[] ToBytes() { return(TLUtils.Combine( TLUtils.SignatureToBytes(Signature), Id.ToBytes(), Parts.ToBytes(), MD5Checksum.ToBytes(), KeyFingerprint.ToBytes())); }
public void EmptyArgConstructorTest() { MD5Checksum target = new MD5Checksum(new byte[0]); byte[] buffer = target.GetBuffer(); Assert.Equal(GetByteArray(0L, 0L), buffer); Assert.Equal(0L, target.HighBits); Assert.Equal(0L, target.LowBits); }
IFileMD5Stats IFileStats.WithMD5(DateTime calculated, MD5Checksum checksum) { calculated = FileStats.NormalizeDateTime(calculated); if (calculated.Equals(_lastCompared)) { return(new FileCompareStatsVerifiedLength(_md5.Length, _md5.LengthLastVerifedUTC, checksum, _groupID, calculated)); } return(new FileCompareStatusReverified(_md5.Length, _md5.LengthLastVerifedUTC, checksum, calculated, _groupID, _lastCompared)); }
IFileMD5Stats IFileStats.WithMD5(DateTime calculated, MD5Checksum checksum) { calculated = FileStats.NormalizeDateTime(calculated); if (calculated.Equals(_fileStats.LengthLastVerifedUTC)) { return(new FileMD5Stats(_fileStats.Length, calculated, _checksum)); } return(new FileMD5StatsVerifiedLength(_fileStats.Length, _fileStats.LengthLastVerifedUTC, _checksum, calculated)); }
public FileCompareStatusReverified(long length, DateTime lastVerified, MD5Checksum checksum, DateTime lastCalculated, Guid groupID, DateTime lastCompared) { _md5 = new FileMD5StatsVerifiedLength(length, lastVerified, checksum, lastCalculated); _lastCompared = FileStats.NormalizeDateTime(lastCompared); if (_md5.LastCalculatedUTC.Equals(_lastCompared)) { throw new InvalidOperationException("Calculated date must differ from verified and compared dates"); } _groupID = groupID; }
public FileMD5StatsVerifiedLength(long length, DateTime lastVerified, MD5Checksum checksum, DateTime lastCalculated) { _fileStats = new FileStats(length, lastVerified); _lastCalculated = FileStats.NormalizeDateTime(lastCalculated); if (_fileStats.LengthLastVerifedUTC.Equals(_lastCalculated)) { throw new InvalidOperationException("Last calculated date must be different than the last verified date"); } _checksum = checksum; }
public FileCompareStatsVerifiedMD5(IFileStats stats, MD5Checksum checksum, Guid groupID, DateTime lastCompared) { _md5 = new FileMD5Stats(stats, checksum); _lastCompared = FileStats.NormalizeDateTime(lastCompared); if (_md5.LastCalculatedUTC.Equals(_lastCompared)) { throw new InvalidOperationException("Last verified date must be different from last calculated date"); } _groupID = groupID; }
public override byte[] ToBytes() { return(TLUtils.Combine( TLUtils.SignatureToBytes(Signature), Id.ToBytes(), Parts.ToBytes(), MD5Checksum.ToBytes(), FileHash.ToBytes(), Secret.ToBytes())); }
public override void ToStream(Stream output) { output.Write(TLUtils.SignatureToBytes(Signature)); Id.ToStream(output); Parts.ToStream(output); MD5Checksum.ToStream(output); Size.ToStream(output); Date.ToStream(output); FileHash.ToStream(output); Secret.ToStream(output); }
public IFileStats ValidateChecksum(long length, MD5Checksum checksum, DateTime lastCalculated) { if (_md5.Length == length) { if (_md5.Checksum.Equals(checksum)) { return(new FileCompareStats(_md5.Length, lastCalculated, _md5.Checksum, _groupID)); } return(new FileMD5Stats(_md5.Length, lastCalculated, _md5.Checksum)); } return(new FileStats(length, lastCalculated)); }
public void ToStringTest() { foreach (long highBits in new long[] { 0L, 0x12345678L, 0x32L }) { string h = highBits.ToString("x16"); foreach (long lowBits in new long[] { 0L, 0x12345678L, 0x32L }) { MD5Checksum target = new MD5Checksum(GetByteArray(lowBits, highBits)); string s = target.ToString(); Assert.Equal(h + lowBits.ToString("x16"), s); } } }
public FileMD5StatsVerifiedLength(IFileMD5Stats stats, DateTime lastVerified) { if (stats == null) { throw new ArgumentNullException("stats"); } _fileStats = new FileStats(stats.Length, lastVerified); _lastCalculated = FileStats.NormalizeDateTime(stats.LastCalculatedUTC); if (_fileStats.LengthLastVerifedUTC.Equals(_lastCalculated)) { throw new InvalidOperationException("Last calculated date must be different than the last verified date"); } _checksum = stats.Checksum; }
public void Array16ArgConstructorTest() { foreach (long highBits in new long[] { 0L, 0x12345678L, 0x32L }) { foreach (long lowBits in new long[] { 0L, 0x12345678L, 0x32L }) { MD5Checksum target = new MD5Checksum(GetByteArray(lowBits, highBits)); byte[] buffer = target.GetBuffer(); Assert.Equal(GetByteArray(lowBits, highBits), buffer); Assert.Equal(highBits, target.HighBits); Assert.Equal(lowBits, target.LowBits); } } }
public FileMD5Stats(long length, DateTime lastVerified, byte[] checksum) { if (checksum == null) { throw new ArgumentNullException("checksum"); } if (checksum.Length == 0) { throw new ArgumentException("Checksum cannot have zero bytes", "checksum"); } _fileStats = new FileStats(length, lastVerified); try { _checksum = new MD5Checksum(checksum); } catch (Exception e) { throw new ArgumentException(e.Message, "checksum", e); } }
public void ParseTest() { foreach (long highBits in new long[] { 0L, 0x12345678L, 0x32L }) { string h = highBits.ToString("x16"); foreach (long lowBits in new long[] { 0L, 0x12345678L, 0x32L }) { MD5Checksum target = MD5Checksum.Parse(h + lowBits.ToString("x16")); byte[] buffer = target.GetBuffer(); Assert.Equal(GetByteArray(lowBits, highBits), buffer); Assert.Equal(highBits, target.HighBits); Assert.Equal(lowBits, target.LowBits); } } }
/// <summary> /// Creates an instance of XMLCache for this type. /// </summary> /// <typeparam name="T">Class the XMLCache object is caching</typeparam> /// <param name="cacheTime">Time in minutes to keep items in cache</param> /// <returns></returns> public XMLCache <T> Create <T>(TimeSpan cacheTime) where T : class { dynamic ret; if (this.xmlCache.TryGetValue(typeof(T), out ret)) { return(ret); } var subDir = Path.Combine(this.rootDirectory.FullName, MD5Checksum.Generate(typeof(T).FullName).ToString()); ret = new XMLCache <T>( new DirectoryInfo(subDir), cacheTime); return((XMLCache <T>) this.xmlCache.GetOrAdd(typeof(T), ret)); //Add to internal list. }
private void WriteSlice(int layerNum, int sliceNum) { MemoryStream slice = this.WorkTaskInfo[layerNum].Slices[sliceNum]; MD5Checksum sliceHash = MD5Checksum.Generate(slice); var currentLayer = this.WorkTaskInfo[layerNum]; // Check if we can reuse other layers position. for (int i = 0; i < this.WorkTaskInfo.Length; i++) { if (i == layerNum) { // Skip if it's current layer. continue; } var otherLayer = this.WorkTaskInfo[i]; if (otherLayer.SlicePosition == -1) { // If this layer have no written slices yet, skip it continue; } if (otherLayer.SlicePosition <= currentLayer.SlicePosition) { // If layers last position is identical, or less, than my last: Skip. // The Anarchy Online planet map viewer does not handle 'backtracking' the stream position too well. continue; } if (otherLayer.SliceHash != sliceHash) { // Not identical, skip. continue; } // Found acceptable, pre-existing slice. // Use this and return. currentLayer.FilePos[sliceNum] = otherLayer.SlicePosition; return; } // We have to write new slice to binfile. int position = (int)this.BinFile.Position; currentLayer.FilePos[sliceNum] = position; currentLayer.SlicePosition = position; currentLayer.SliceHash = sliceHash; slice.WriteTo(this.BinFile); }
public FileMD5Stats(IFileStats stats, byte[] checksum) { if (stats == null) { throw new ArgumentNullException("status"); } if (checksum == null) { throw new ArgumentNullException("checksum"); } if (checksum.Length == 0) { throw new ArgumentException("Checksum cannot have zero bytes", "checksum"); } _fileStats = (stats is FileStats) ? (FileStats)stats : new FileStats(stats.Length, stats.LengthLastVerifedUTC); try { _checksum = new MD5Checksum(checksum); } catch (Exception e) { throw new ArgumentException(e.Message, "checksum", e); } }
/// <summary> /// Generates a hash from the provided identifiers /// </summary> /// <param name="identifiers"></param> /// <returns></returns> public static MD5Checksum GetChecksum(object[] identifiers) { if (identifiers == null) { throw new ArgumentNullException("identifiers"); } if (identifiers.Length == 0) { throw new ArgumentException("You must provide at least one identifier for the cache entry.", "identifiers"); } var sb = new StringBuilder(); foreach (var id in identifiers) { sb.Append(id.ToString()); } return(MD5Checksum.Generate(sb.ToString())); }
public FileMD5StatsVerifiedLength(long length, DateTime lastVerified, byte[] checksum, DateTime lastCalculated) { if (checksum == null) { throw new ArgumentNullException("checksum"); } if (checksum.Length == 0) { throw new ArgumentException("Checksum cannot have zero bytes", "checksum"); } _fileStats = new FileStats(length, lastVerified); _lastCalculated = FileStats.NormalizeDateTime(lastCalculated); if (_fileStats.LengthLastVerifedUTC.Equals(_lastCalculated)) { throw new InvalidOperationException("Last calculated date must be different than the last verified date"); } try { _checksum = new MD5Checksum(checksum); } catch (Exception e) { throw new ArgumentException(e.Message, "checksum", e); } }
public bool GetObject(StreamDataParserTask task, out object value) { if (task.StreamType == typeof(MD5Checksum)) { // 128 bits value = new MD5Checksum(task.Stream.ReadBytes(16)); return true; } if (task.StreamType == typeof(SHA1Checksum)) { // 160 bits value = new SHA1Checksum(task.Stream.ReadBytes(20)); return true; } if (task.StreamType == typeof(SHA256Checksum)) { // 256 bits value = new SHA256Checksum(task.Stream.ReadBytes(32)); return true; } if (task.StreamType == typeof(SHA384Checksum)) { // 384 bits value = new SHA256Checksum(task.Stream.ReadBytes(48)); return true; } if (task.StreamType == typeof(SHA512Checksum)) { // 512 bits value = new SHA512Checksum(task.Stream.ReadBytes(64)); return true; } value = null; return false; }
/// <summary> /// Add data to the image archive /// </summary> /// <param name="Key"></param> /// <param name="Bytes"></param> public void Add(string Key, byte[] Bytes) { lock (this) { if (this.archiveIndexKey.ContainsKey(Key)) { throw new Exception("Key already exists: " + Key); } MD5Checksum md5 = MD5Checksum.Generate(Bytes); ImageArchiveEntry entry; if (this.archiveIndexMD5.ContainsKey(md5)) { ImageArchiveEntry oldiae = this.archiveIndexMD5[md5]; entry = new ImageArchiveEntry { Key = Key, BytePositionStart = oldiae.BytePositionStart, Size = oldiae.Size, MD5 = md5 }; } else { entry = new ImageArchiveEntry { Key = Key, BytePositionStart = this.archiveFile.Position, Size = Bytes.Length, MD5 = md5 }; //Add the entry to the MD5 index. this.archiveIndexMD5.Add(md5, entry); //Add to memorystream this.archiveFile.Write(Bytes, 0, Bytes.Length); } //Add to key index this.archiveIndexKey.Add(Key, entry); } }
internal FsFileData(long length, MD5Checksum checksum) : this(length) { ComparisonInfo = checksum; }