private IReliableDictionary <string, byte[]> GetReminderDictionary(ActorId actorId) { var bytes = Encoding.UTF8.GetBytes(actorId.GetStorageKey()); var storageIdx = CRC64.ToCRC64(bytes) % (ulong)this.reminderDictionaries.Length; return(this.reminderDictionaries[storageIdx]); }
// I/O // From byte buffer public void Import(byte[] buffer) { Destroy(); // Generate a ulong GUID m_Guid = CRC64.Compute(buffer); // Read the buffer using (MemoryStream ms = new MemoryStream(buffer)) { m_TexData = new byte[(int)(ms.Length)]; ms.Read(m_TexData, 0, (int)(ms.Length)); } m_Tex = new Texture2D(4, 4); if (!m_Tex.LoadImage(m_TexData)) { Destroy(); Texture2D tex = Resources.Load(s_DefaultTexPath) as Texture2D; m_TexData = tex.EncodeToPNG(); m_Tex = new Texture2D(4, 4); if (!m_Tex.LoadImage(m_TexData)) { Debug.LogError("Can't find default decal texture!"); Destroy(); m_Tex = new Texture2D(16, 16, TextureFormat.ARGB32, false); m_TexData = m_Tex.EncodeToPNG(); } } m_Tex.filterMode = FilterMode.Trilinear; m_Tex.wrapMode = TextureWrapMode.Clamp; }
void EndSendConnect(IAsyncResult ar) { try { _client.EndConnect(ar); //_status = EFileStatus.CHECKING; _fileData.HashCode = CRC64.Compute(_stream); _stream.Position = 0; //_status = EFileStatus.SENDING; NetworkStream netStream = _client.GetStream(); BinaryWriter bw = new BinaryWriter(netStream); byte head = 0xDD; bw.Write(head); bw.Write(_fileData.HashCode); bw.Write(_fileData.FileName); bw.Write(_fileData.FileLength); bw.Write(_account); ReadStream(); } catch (Exception e) { Close(); LogManager.Error(e); } }
// Use this for initialization void Start() { // Initialize CRC Generator crcGenerator = new CRC64(); crcPolynomial = Convert.ToUInt64(crcPolynomialBinary, 2); crcGenerator.Init(crcPolynomial); // Load settings from file FileInfo iniFile = new FileInfo("config.txt"); StreamReader reader = iniFile.OpenText(); string text = reader.ReadLine(); if (text != null) { ipString = text; } text = reader.ReadLine(); if (text != null) { port = int.Parse(text); } // TODO: Connect on prompt rather than application start StartupClient(); }
private IEnumerable <KeyData <TKey, TValue> > ReadBlock(int blockSize) { var blockStartPosition = checked ((int)this.reader.BaseStream.Position); var alignedBlockStartPosition = blockStartPosition - KeyChunkMetadata.Size; this.reader.BaseStream.Position = alignedBlockStartPosition + blockSize - sizeof(ulong); var expectedChecksum = this.reader.ReadUInt64(); this.reader.BaseStream.Position = blockStartPosition; // Verify checksum. var actualChecksum = CRC64.ToCRC64(this.reader.BaseStream.GetBuffer(), alignedBlockStartPosition, blockSize - sizeof(ulong)); if (actualChecksum != expectedChecksum) { throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, SR.Error_KeyCheckpoint_FailedToRead_Checksum, actualChecksum, expectedChecksum)); } while (this.reader.BaseStream.Position < (alignedBlockStartPosition + blockSize - sizeof(ulong))) { var keyData = this.keyCheckpointFile.ReadKey <TKey, TValue>(this.reader, this.keySerializer); yield return(keyData); } Diagnostics.Assert( this.reader.BaseStream.Position == (alignedBlockStartPosition + blockSize - sizeof(ulong)), this.traceType, "Failed to read block of keys due to block alignment mismatch."); }
private void WriteValue <TValue>(InMemoryBinaryWriter memoryBuffer, long basePosition, TVersionedItem <TValue> item, byte[] value) { // Deleted items don't have values. Only serialize valid items. if (item.Kind != RecordKind.DeletedVersion) { // WriteItemAsync valueSerializer followed by checksum. // Serialize the value. var valueStartPosition = memoryBuffer.BaseStream.Position; memoryBuffer.Write(value); var valueEndPosition = memoryBuffer.BaseStream.Position; Diagnostics.Assert( valueEndPosition >= valueStartPosition, DifferentialStoreConstants.TraceType, "User's value IStateSerializer moved the stream position backwards unexpectedly!"); // Write the checksum of just that value's bytes. var valueSize = checked ((int)(valueEndPosition - valueStartPosition)); var checksum = CRC64.ToCRC64(memoryBuffer.BaseStream.GetBuffer(), checked ((int)valueStartPosition), valueSize); // Update the in-memory offset and size for this item. item.Offset = basePosition + valueStartPosition; item.ValueSize = valueSize; item.ValueChecksum = checksum; // Update checkpoint file in-memory metadata. this.Properties.ValueCount++; } // Update the in-memory metadata about which file this key-value exists in on disk. item.FileId = this.FileId; }
public static String ComputeCRC64(byte[] bytes) { using (var hashAlgorithmImpl = new CRC64()) { var hashBytes = hashAlgorithmImpl.ComputeHash(bytes); return(String.Concat(hashBytes.Select(b => b.ToString("x2")))); } }
public static String ComputeCrc64(this byte[] bytes, HashFormat hashFormat) { using (var hashImpl = new CRC64()) { var hashBytes = hashImpl.ComputeHash(bytes); return(ConvertToString(hashBytes, hashFormat)); } }
public static string GetFeedsIdentifier(ceDomainConfigEx domain) { if (domain != null) { return(CRC64.ComputeAsUtf8String(domain.GetCfg(CE.DomainConfig.ISoftBet.FlashGameFeedsURL) + domain.GetCfg(CE.DomainConfig.ISoftBet.HTML5GameFeedsURL)).ToString()); } return(null); }
public async Task WriteAsync() { var filePath = Path.Combine(this.directory, this.FileName); using (var stream = FabricFile.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan)) { var versionArraySegment = new ArraySegment <byte>(BitConverter.GetBytes(FileVersion)); if (this.CurrentCheckpoint != null) { ArraySegment <byte> currentCheckpointFileNameArraySegment; using (var writer = new InMemoryBinaryWriter(new MemoryStream())) { writer.Write(this.CurrentCheckpoint.FileName); currentCheckpointFileNameArraySegment = new ArraySegment <byte>(writer.BaseStream.GetBuffer(), 0, (int)writer.BaseStream.Position); } var currentCheckpointFileNameLengthArraySegment = new ArraySegment <byte>(BitConverter.GetBytes(currentCheckpointFileNameArraySegment.Count)); var crc = CRC64.ToCRC64(new[] { versionArraySegment, currentCheckpointFileNameLengthArraySegment, currentCheckpointFileNameArraySegment }); var crcArraySegment = new ArraySegment <byte>(BitConverter.GetBytes(crc)); await stream.WriteAsync(versionArraySegment.Array, versionArraySegment.Offset, versionArraySegment.Count).ConfigureAwait(false); await stream.WriteAsync( currentCheckpointFileNameLengthArraySegment.Array, currentCheckpointFileNameLengthArraySegment.Offset, currentCheckpointFileNameLengthArraySegment.Count).ConfigureAwait(false); await stream.WriteAsync( currentCheckpointFileNameArraySegment.Array, currentCheckpointFileNameArraySegment.Offset, currentCheckpointFileNameArraySegment.Count).ConfigureAwait(false); await stream.WriteAsync(crcArraySegment.Array, crcArraySegment.Offset, crcArraySegment.Count).ConfigureAwait(false); } else { var currentCheckpointNameLengthArraySegment = new ArraySegment <byte>(BitConverter.GetBytes(0)); var crc = CRC64.ToCRC64(new[] { versionArraySegment, currentCheckpointNameLengthArraySegment }); var crcArraySegment = new ArraySegment <byte>(BitConverter.GetBytes(crc)); await stream.WriteAsync(versionArraySegment.Array, versionArraySegment.Offset, versionArraySegment.Count).ConfigureAwait(false); await stream.WriteAsync( currentCheckpointNameLengthArraySegment.Array, currentCheckpointNameLengthArraySegment.Offset, currentCheckpointNameLengthArraySegment.Count).ConfigureAwait(false); await stream.WriteAsync(crcArraySegment.Array, crcArraySegment.Offset, crcArraySegment.Count).ConfigureAwait(false); } } }
static void Main(string[] args) { string text = "1212"; byte[] bytes = Encoding.UTF8.GetBytes(text); var crc64 = CRC64.Create("CRC64_XZ"); var crc64Value = crc64.Compute(bytes); Console.WriteLine("0x" + crc64Value.ToString("X2")); }
public static String GenerateHex64() { using (var crc64 = new CRC64()) { var uniqText = String.Concat(Guid.NewGuid(), Guid.NewGuid(), Environment.TickCount); var srcBytes = Encoding.UTF32.GetBytes(uniqText); var hashBytes = crc64.ComputeHash(srcBytes); return(String.Join(String.Empty, hashBytes.Select(b => b.ToString("x2")))); } }
public static ulong GenerateInt64() { using (var crc64 = new CRC64()) { var uniqText = String.Concat(Guid.NewGuid(), Guid.NewGuid(), Environment.TickCount); var srcBytes = Encoding.UTF32.GetBytes(uniqText); var hashBytes = crc64.ComputeHash(srcBytes); return(BitConverter.ToUInt64(hashBytes, 0)); } }
public sealed override UInt64 CalculateChecksum() { var globals = CRC64.Calculate(0, (Byte *)_globals, sizeof(_globals_)); var entities = CRC64.Calculate(0, (Byte *)_entities, sizeof(_entities_)); unchecked { UInt64 hash = 17UL; hash = hash * 31UL + globals; hash = hash * 31UL + entities; return(hash); } }
private static ulong GetLiveCasinoTableStatusHash(ceDomainConfigEx domain) { StringBuilder sb = new StringBuilder(); List <ceLiveCasinoTableBaseEx> tables = LiveCasinoTableAccessor.GetDomainTables(domain.DomainID, null, true, true) .OrderBy(t => t.ID).ToList(); foreach (ceLiveCasinoTableBaseEx table in tables) { sb.AppendFormat("{0}-{1}\n", table.ID, table.IsOpen(domain.DomainID)); } return(CRC64.ComputeAsAsciiString(sb.ToString())); }
internal static void LoadSteamItems() { _SteamItems.Clear(); string[] fileNames = Directory.GetFiles(VCConfig.s_CreationNetCachePath, "*" + VCConfig.s_CreationNetCacheFileExt); foreach (string name in fileNames) { FileInfo file = new FileInfo(name); Stream stream = file.OpenRead(); ulong hashCode = CRC64.Compute(stream); string[] nameSplit = file.Name.Split('.'); _SteamItems[hashCode] = nameSplit[0]; } }
void OnDownLoadFileCallBack(byte[] fileData, PublishedFileId_t p_id, bool bOK, int index = -1, int dungeonId = -1) { if (isActve == false) { return; } //bool DonLoadSucess = false; if (bOK) { if (mItemsMap.ContainsKey(p_id)) { if (mItemsMap[p_id] != null) { SteamPreFileAndVoteDetail detail = mItemsMap[p_id]; VCIsoHeadData headData; string creation = "Object"; if (VCIsoData.ExtractHeader(fileData, out headData) > 0) { creation = headData.Category.ToString(); creation = creation.Substring(2, creation.Length - 2); } string downLoadFilePath = VCConfig.s_IsoPath + string.Format("/Download/{0}/", creation); string netCacheFilePath = VCConfig.s_CreationNetCachePath; string downLoadFileName = detail.m_rgchTitle; string netCacheFileName = CRC64.Compute(fileData).ToString(); if (SaveToFile(fileData, downLoadFileName, downLoadFilePath, VCConfig.s_IsoFileExt)) { UIWorkShopCtrl.AddDownloadFileName(mItemsMap[p_id].m_rgchTitle + VCConfig.s_IsoFileExt, mIsPrivate); mDownMap[p_id] = 100; } //lz-2016.05.30 保存一份到NetCache路径下,避免NetCache重复下载 SaveToFile(fileData, netCacheFileName, netCacheFilePath, VCConfig.s_CreationNetCacheFileExt); } } } else { mDownMap[p_id] = -1; MessageBox_N.ShowOkBox(PELocalization.GetString(8000493)); } // if (e_DownLoadFile != null) // e_DownLoadFile(filePath,DonLoadSucess); }
void RecveiveCallback(IAsyncResult ar) { try { NetworkStream netStream = _client.GetStream(); int recvLength = netStream.EndRead(ar); _stream.Write(_buffer, 0, recvLength); _pos += recvLength; if (_pos < _fileData.FileLength) { netStream.BeginRead(_buffer, 0, FileConst.BlockSize, new AsyncCallback(RecveiveCallback), null); } else { _stream.Position = 0; ulong hashCode = CRC64.Compute(_stream); bool successed = hashCode == _fileData.HashCode; if (null != _stream) { _stream.Close(); _stream = null; } if (successed) { string fileName = _path + _fileData.FileName; if (File.Exists(fileName)) { File.Delete(fileName); } string fileTmp = _path + _fileData.FileName + ".tmp"; File.Move(fileTmp, fileName); } if (null != CompleteEvent) { CompleteEvent(this, new FileCompleteEventArgs(successed)); } } } catch (Exception e) { Close(); throw e; } }
public static ProjectData.HashList <ulong> LoadListsSubFatNames(this ProjectData.Project project, int bigVersion) { if (bigVersion >= 9) // TODO: check if this is right... { return(project.LoadLists("*.subfatlist", a => CRC64.Hash(a.ToLowerInvariant()), s => s.Replace("/", "\\"))); } return(project.LoadLists("*.subfatlist", a => (ulong)CRC32.Hash(a.ToLowerInvariant()), s => s.Replace("\\", "/"))); }
public static ProjectData.HashList <ulong> LoadListsFileNames(this ProjectData.Manager manager, int bigVersion) { if (bigVersion >= 9) // TODO: check if this is right... { return(manager.LoadLists("*.filelist", a => CRC64.Hash(a.ToLowerInvariant()), s => s.Replace("/", "\\"))); } return(manager.LoadLists("*.filelist", a => (ulong)CRC32.Hash(a.ToLowerInvariant()), s => s.Replace("\\", "/"))); }
protected void GetHeaderCRC() { if (!_validCrc) { _validCrc = CRC64.Hash(0, Encoding.ASCII.GetBytes("123456789")) == 0xe9c6d914c4b8d9ca; if (!_validCrc) { throw new Exception("CRC64 is invalid!"); } } byte[] buffer = new byte[36]; Stream.Read(buffer, 0, 36); _headerCrc = CRC64.Hash(0, buffer); }
public static void SendIsoDataToServer(string name, string desc, byte[] preIso, byte[] isoData, string[] tags, bool sendToServer = true, ulong fileId = 0, bool free = false) { #if PLANET_EXPLORERS //ulong hash_code = CRC64.Compute(isodata); //FileTransManager.RegisterISO(hash_code, isodata); #endif #if SteamVersion SteamWorkShop.SendFile(null, name, desc, preIso, isoData, tags, sendToServer, -1, fileId, free); #else ulong hash_code = CRC64.Compute(isoData); //FileTransManager.RegisterISO(hash_code, isoData); #endif }
private static async Task CopyListElementsFrameAsync(Stream inputStream, Stream outputStream, string traceType) { var listElementsCountSegment = new ArraySegment <byte>(new byte[sizeof(int)]); var listElementsBytesSegment = new ArraySegment <byte>(new byte[sizeof(int)]); var crcSegment = new ArraySegment <byte>(new byte[sizeof(ulong)]); var listElementsCount = await SerializationHelper.ReadIntAsync(listElementsCountSegment, inputStream).ConfigureAwait(false); if (listElementsCount < 0) { var exc = new InvalidDataException(string.Format("CheckpointFileHelper.CopyListElementsFrameAsync : Unexpected listElementsCount: {0}", listElementsCount)); FabricEvents.Events.ReliableConcurrentQueue_ExceptionError(traceType, exc.ToString()); throw exc; } var listElementsBytes = await SerializationHelper.ReadIntAsync(listElementsBytesSegment, inputStream).ConfigureAwait(false); if (listElementsBytes < 0) { var exc = new InvalidDataException(string.Format("CheckpointFileHelper.CopyListElementsFrameAsync : Unexpected listElementsBytes: {0}", listElementsBytes)); FabricEvents.Events.ReliableConcurrentQueue_ExceptionError(traceType, exc.ToString()); throw exc; } var listElementsSegment = new ArraySegment <byte>(new byte[listElementsBytes]); await SerializationHelper.ReadBytesAsync(listElementsSegment, listElementsBytes, inputStream).ConfigureAwait(false); await SerializationHelper.ReadBytesAsync(crcSegment, sizeof(ulong), inputStream).ConfigureAwait(false); var readCRC = BitConverter.ToUInt64(crcSegment.Array, crcSegment.Offset); var calcCrc = CRC64.ToCRC64(new[] { listElementsCountSegment, listElementsBytesSegment, listElementsSegment }); if (calcCrc != readCRC) { var exc = new InvalidDataException(string.Format("CheckpointFileHelper.CopyListElementsFrameAsync => CRC mismatch. Read: {0} Calculated: {1}", readCRC, calcCrc)); FabricEvents.Events.ReliableConcurrentQueue_ExceptionError(traceType, exc.ToString()); throw exc; } await outputStream.WriteAsync(listElementsCountSegment.Array, listElementsCountSegment.Offset, listElementsCountSegment.Count).ConfigureAwait(false); await outputStream.WriteAsync(listElementsBytesSegment.Array, listElementsBytesSegment.Offset, listElementsBytesSegment.Count).ConfigureAwait(false); await outputStream.WriteAsync(listElementsSegment.Array, listElementsSegment.Offset, listElementsSegment.Count).ConfigureAwait(false); await outputStream.WriteAsync(crcSegment.Array, crcSegment.Offset, crcSegment.Count).ConfigureAwait(false); }
// Create decal data file public static bool CreateDecalDataFile(VCDecalAsset vcdcl) { try { byte[] buffer = vcdcl.Export(); ulong guid = CRC64.Compute(buffer); string sguid = guid.ToString("X").PadLeft(16, '0'); FileStream fs = new FileStream(VCConfig.s_DecalPath + sguid + VCConfig.s_DecalFileExt, FileMode.Create, FileAccess.ReadWrite); fs.Write(buffer, 0, buffer.Length); fs.Close(); return(true); } catch (Exception) { return(false); } }
// Calculate a group of made-material's hash code (64 bit) public static ulong CalcMatGroupHash(VAMaterial[] mats) { string mat_guids = ""; for (int i = 0; i < mats.Length; i++) { if (mats[i] != null) { mat_guids += mats[i].m_Guid.ToString(); } else { mat_guids += "null"; } } return(CRC64.Compute(System.Text.Encoding.UTF8.GetBytes(mat_guids))); }
// Load Resource public bool LoadRes() { string fn1 = VCConfig.s_CreationPath + HashString + VCConfig.s_CreationFileExt; string fn2 = VCConfig.s_CreationNetCachePath + HashString + VCConfig.s_CreationNetCacheFileExt; string fn = ""; if (File.Exists(fn1)) { fn = fn1; } else if (File.Exists(fn2)) { fn = fn2; } if (fn.Length == 0) { return(false); } try { using (FileStream fs = new FileStream(fn, FileMode.Open)) { m_Resource = new byte [(int)(fs.Length)]; fs.Read(m_Resource, 0, (int)(fs.Length)); fs.Close(); } #if SteamVersion #else if (m_HashCode != CRC64.Compute(m_Resource)) { Debug.LogError("Load Creation Resource Error: \r\nContent doesn't match the hash code"); return(false); } #endif return(ReadRes()); } catch (Exception e) { Debug.LogError("Load Creation Resource Error: \r\n" + e); return(false); } }
public static async Task <IEnumerable <IListElement <T> > > ReadAsync(Stream stream, IStateSerializer <T> stateSerializer, string traceType) { var listElementsCountSegment = new ArraySegment <byte>(new byte[sizeof(int)]); var listElementsBytesSegment = new ArraySegment <byte>(new byte[sizeof(int)]); var listElementsCount = await SerializationHelper.ReadIntAsync(listElementsCountSegment, stream).ConfigureAwait(false); if (listElementsCount < 0) { throw new InvalidDataException(string.Format("Unexpected listElementsCount: {0}", listElementsCount)); } var listElementsBytes = await SerializationHelper.ReadIntAsync(listElementsBytesSegment, stream).ConfigureAwait(false); if (listElementsBytes < 0) { throw new InvalidDataException(string.Format("Unexpected listElementsBytes: {0}", listElementsBytes)); } using (var reader = new InMemoryBinaryReader(new MemoryStream())) { reader.BaseStream.SetLength(listElementsBytes + sizeof(ulong)); await SerializationHelper.ReadBytesAsync(new ArraySegment <byte>(reader.BaseStream.GetBuffer()), listElementsBytes + sizeof(ulong), stream) .ConfigureAwait(false); var listElements = new IListElement <T> [listElementsCount]; for (var i = 0; i < listElementsCount; i++) { var id = reader.ReadInt64(); var value = stateSerializer.Read(reader); // if this tries to read beyond the end of the stream, listElementsBytes was incorrect (too small) listElements[i] = DataStore <T> .CreateQueueListElement(id, value, traceType, ListElementState.EnqueueApplied); } var readCRC = reader.ReadUInt64(); var calcCRC = CRC64.ToCRC64(new[] { listElementsCountSegment, listElementsBytesSegment, new ArraySegment <byte>(reader.BaseStream.GetBuffer(), 0, listElementsBytes), }); if (readCRC != calcCRC) { throw new InvalidDataException(string.Format("CRC mismatch. Read: {0} Calculated: {1}", readCRC, calcCRC)); } return(listElements); } }
// Use this for initialization void Start() { crcGenerator = new CRC64(); crcPolynomial = Convert.ToUInt64(crcPolynomialBinary, 2); crcGenerator.Init(crcPolynomial); //byte[] bytes = new byte[1024]; try { // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. IPAddress ipAddress = IPAddress.Parse(ipString); remoteEP = new IPEndPoint(ipAddress, 18944); // Create a TCP/IP socket. sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { // Connect the socket to the remote endpoint. Catch any errors. sender.Connect(remoteEP); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}", se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public async Task <byte[]> ReadValueAsync <TValue>(TVersionedItem <TValue> item) { Stream fileStream = null; try { // Acquire a re-usable file stream for exclusive use during this read. fileStream = this.ReaderPool.AcquireStream(); var snapFileStream = fileStream as FileStream; Diagnostics.Assert(snapFileStream != null, this.traceType, "fileStream must be a FileStream"); Microsoft.ServiceFabric.Replicator.Utility.SetIoPriorityHint(snapFileStream.SafeFileHandle, this.priorityHint); // TODO: use memory stream pool here. using (var stream = new MemoryStream(capacity: item.ValueSize)) { // Read the value bytes and the checksum into memory. fileStream.Position = item.Offset; stream.SetLength(item.ValueSize); await fileStream.ReadAsync(stream.GetBuffer(), 0, item.ValueSize).ConfigureAwait(false); // Read the checksum from memory. stream.Position = item.ValueSize; var checksum = item.ValueChecksum; // Re-compute the checksum. var expectedChecksum = CRC64.ToCRC64(stream.GetBuffer(), 0, item.ValueSize); if (checksum != expectedChecksum) { throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, SR.Error_FailedReadValue_ChecksumMismatch_TwoArgs, checksum, expectedChecksum)); } return(stream.GetBuffer()); } } finally { // Return the file stream to the pool for re-use. this.ReaderPool.ReleaseStream(fileStream); } }
protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { if (CRC64 != null) { CRC64.Dispose(); } if (MD5 != null) { MD5.Dispose(); } } disposed = true; }