/// <summary> /// Gets the string representation of SHA1 of a byte data array. /// </summary> /// <param name="data"></param> /// <returns></returns> public static string GetSHA1(byte[] data) { using (SHA1 sha1 = SHA1.Create()) { return(StringGetter.GetHexString(sha1.ComputeHash(data))); } }
/// <summary> /// Gets the string representation of MD5 of a byte data array. /// </summary> /// <param name="data"></param> /// <returns></returns> public static string GetMD5(byte[] data) { using (MD5 md5 = MD5.Create()) { return(StringGetter.GetHexString(md5.ComputeHash(data))); } }
public async Task <List <TreeNodeUI> > GetNodesAsync(string alias, ParentPath?parentPath) { var collection = _collectionProvider.GetCollection(alias); if (collection == null) { throw new InvalidOperationException($"Failed to get collection for given alias ({alias})."); } var parent = await _parentService.GetParentAsync(parentPath); if (collection.TreeView?.EntityVisibility == EntityVisibilty.Visible) { // TODO: pagination var query = Query.TakeElements(25); var entities = await collection.Repository.GetAllAsync(parent, query); return(await entities.ToListAsync(async entity => { var entityVariant = collection.GetEntityVariant(entity); var node = new TreeNodeUI { Id = entity.Id, Name = collection.TreeView.Name !.StringGetter.Invoke(entity), RootVisibleOfCollections = collection.Collections.All(subCollection => subCollection.TreeView?.RootVisibility == CollectionRootVisibility.Visible), Collections = collection.Collections.ToList(subCollection => subCollection.Alias) }; var editAuthorizationChallenge = await _authorizationService.AuthorizeAsync( _httpContextAccessor.HttpContext.User, entity, Operations.Update); if (editAuthorizationChallenge.Succeeded) { node.Path = UriHelper.Node(Constants.Edit, collection.Alias, entityVariant, parentPath, entity.Id); } else { var viewAuthorizationChallenge = await _authorizationService.AuthorizeAsync( _httpContextAccessor.HttpContext.User, entity, Operations.Read); if (viewAuthorizationChallenge.Succeeded) { node.Path = UriHelper.Node(Constants.View, collection.Alias, entityVariant, parentPath, entity.Id); } } return node; })); } else { return(new List <TreeNodeUI>()); } }
/// <summary> /// Gets the string representation of SHA1 /// of a string data using a specific encoding. /// </summary> /// <param name="data"></param> /// <param name="encoding"></param> /// <returns></returns> public static string GetSHA1(string data, Encoding encoding) { using (SHA1 sha1 = SHA1.Create()) { var hash = sha1.ComputeHash(encoding.GetBytes(data)); return(StringGetter.GetHexString(hash)); } }
protected override object RequestComAddInAutomationService() { if (myAddIn == null) { myAddIn = new StringGetter(); } return(myAddIn); }
public static string GetMD5(string data, Encoding encoding) { using (MD5 md5 = MD5.Create()) { var d = encoding.GetBytes(data); var hashBytes = md5.ComputeHash(d); return(StringGetter.GetHexString(hashBytes)); } }
// local protected int LoadHudData(StringGetter cb, UnityEvent datachange, UnityEvent datadone, string cap, string donecap, int slot) { #if FULLLOG MoreDebug.Log("go: " + gameObject.name); #endif HudBridgeElement data = new HudBridgeElement(); data.InitFromBridge(cb, datachange, datadone, cap, donecap, slot); hudkeys.Add(hud.RegisterHudData(data)); data.ForceUpdate(); return(hudkeys.Count - 1); }
public void FromBase64StringTest(string base64, string fact, CodePage cp = CodePage.Default) { if (cp == CodePage.Default) { Assert.Equal(fact, StringGetter.FromBase64String(base64)); } else { Assert.Equal(fact, StringGetter.FromBase64String(base64, cp)); Assert.Equal(fact, StringGetter.FromBase64String(base64, EncodingUtil.GetEncoding(cp))); } }
public void GetHexStringTest() { byte[] data0 = { 0x5a, 0x4b }; const string fact0 = "5A4B"; Assert.Equal(fact0, StringGetter.GetHexString(data0)); byte[] data1 = { 0x06, 0x11, 0xab, 0xca, 0xfe }; const string fact1 = "06.11.ab.ca.fe"; Assert.Equal(fact1, StringGetter.GetHexString(data1, "."), true); }
public void Delegate_CreateDelegate_StaticCriticalProperty() { PropertyInfo pi = typeof(Assembly).GetProperty("CodeBase"); MethodInfo miget = pi.GetGetMethod(); StringGetter dget = null; Assert.IsNotNull(miget, "CurrentDirectory"); Assert.Throws <ArgumentException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget); }, "Delegate.CreateDelegate(Type,MethodInfo)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget, false); Assert.IsNull(dget, "Delegate.CreateDelegate(Type,MethodInfo,bool)"); Assert.Throws <ArgumentException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, miget); }, "Delegate.CreateDelegate(Type,object,MethodInfo)"); Assert.Throws <ArgumentNullException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_CodeBase"); }, "Delegate.CreateDelegate(Type,object,string)"); Assert.Throws <ArgumentException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Assembly), "get_CodeBase"); }, "Delegate.CreateDelegate(Type,Type,string)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, miget, false); Assert.IsNull(dget, "Delegate.CreateDelegate(Type,object,MethodInfo,bool)"); Assert.Throws <ArgumentNullException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_codebase", true); }, "Delegate.CreateDelegate(Type,object,string,bool)"); Assert.Throws <ArgumentException> (delegate { // delegate could not be created dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Assembly), "get_codebase", true); }, "Delegate.CreateDelegate(Type,Type,string,bool)"); Assert.Throws <ArgumentNullException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_codebase", true, false); }, "Delegate.CreateDelegate(Type,object,MethodInfo,bool,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Assembly), "get_codebase", true, false); Assert.IsNull(dget, "Delegate.CreateDelegate(Type,Type,MethodInfo,bool,bool)"); }
public void InitFromBridge(StringGetter cb, UnityEvent dc, UnityEvent di, string cap, string orv, int ds) { #if FULLLOG MoreDebug.Log("InitFromBridge"); #endif Callback = cb; onDataChange = dc; onDataInvalidated = di; Caption = cap; OverrideValue = orv; DisplaySlot = ds; onDataChange.AddListener(UpdateFromData); onDataInvalidated.AddListener(InvalidateDataSource); }
public void Delegate_CreateDelegate_InstanceTransparentProperty() { X509Certificate cert = new X509Certificate(); PropertyInfo pi = typeof(X509Certificate).GetProperty("Issuer"); MethodInfo miget = pi.GetGetMethod(); StringGetter dget = null; Assert.Throws <ArgumentException> (delegate { // Delegate.CreateDelegate(Type,MethodInfo) is for static members only dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget); }, "Delegate.CreateDelegate(Type,MethodInfo)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget, false); Assert.IsNull(dget, "Delegate.CreateDelegate(Type,MethodInfo,boolean)"); // not static dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), cert, miget); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,MethodInfo)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), cert, "get_Issuer"); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,string)"); Assert.Throws <ArgumentException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(X509Certificate), "get_Issuer"); }, "Delegate.CreateDelegate(Type,Type,string)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), cert, miget, false); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,MethodInfo,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), cert, "get_issuer", true); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,string,bool)"); Assert.Throws <ArgumentException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(X509Certificate), "get_issuer", true); }, "Delegate.CreateDelegate(Type,Type,string,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), cert, "get_issuer", true, false); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,MethodInfo,bool,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(X509Certificate), "get_issuer", true, false); Assert.IsNull(dget, "Delegate.CreateDelegate(Type,Type,MethodInfo,bool,bool)"); }
public void GetHumanReadableByteCountTest() { const long num0 = 1024L; const string fact0 = "1.0 KiB"; Assert.Equal(fact0, StringGetter.GetHumanReadableByteCount(num0)); const long num1 = 16L; const string fact1 = "16 B"; Assert.Equal(fact1, StringGetter.GetHumanReadableByteCount(num1)); const long num2 = 1000000L; const string fact2 = "1.00 MB"; Assert.Equal(fact2, StringGetter.GetHumanReadableByteCount(num2, true, 2)); const long num3 = 1_132_951_587L; const string fact3 = "1.055 GiB"; Assert.Equal(fact3, StringGetter.GetHumanReadableByteCount(num3, false, 3)); }
public void Delegate_CreateDelegate_StaticTransparentProperty() { PropertyInfo pi = typeof(Environment).GetProperty("NewLine"); MethodInfo miget = pi.GetGetMethod(); StringGetter dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget); Assert.IsNotNull(dget(), "Delegate.CreateDelegate(Type,MethodInfo)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), miget, false); Assert.IsNotNull(dget(), "Delegate.CreateDelegate(Type,MethodInfo,boolean)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, miget); Assert.IsNotNull(dget(), "Delegate.CreateDelegate(Type,object,MethodInfo)"); Assert.Throws <ArgumentNullException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_NewLine"); }, "Delegate.CreateDelegate(Type,Type,string)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Environment), "get_NewLine"); Assert.IsNotNull(dget(), "Delegate.CreateDelegate(Type,Type,string)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, miget, false); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,MethodInfo,bool)"); Assert.Throws <ArgumentNullException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_newline", true); }, "Delegate.CreateDelegate(Type,object,string,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Environment), "get_newline", true); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,Type,string,bool)"); Assert.Throws <ArgumentNullException> (delegate { dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), null, "get_newline", true, false); }, "Delegate.CreateDelegate(Type,object,MethodInfo,bool,bool)"); dget = (StringGetter)Delegate.CreateDelegate(typeof(StringGetter), typeof(Environment), "get_newline", true, false); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,Type,MethodInfo,bool,bool)"); }
/// <summary> /// Loads the storage from a <see cref="System.IO.Stream"/>. /// </summary> /// <param name="stream"> /// The <see cref="System.IO.Stream"/> from which the storage should be loaded. /// </param> /// <param name="flags"> /// The <see cref="DBFilesClient.NET.LoadFlags"/> to be used when loading. /// </param> public unsafe virtual void Load(Stream stream, LoadFlags flags) { GenerateLoadMethod(); byte[] headerBytes; byte[] data; byte[] pool; fixed(byte *headerPtr = headerBytes = new byte[DBCHeader.Size]) { if (stream.Read(headerBytes, 0, DBCHeader.Size) != DBCHeader.Size) { throw new IOException("Failed to read the DBC header."); } var header = (DBCHeader *)headerPtr; if (!flags.HasFlag(LoadFlags.IgnoreWrongFourCC) && header->FourCC != 0x43424457) { throw new ArgumentException("This is not a valid DBC file."); } if (header->RecordSize != m_entrySize) { throw new ArgumentException("This DBC file has wrong record size (" + header->RecordSize + ", expected is " + m_entrySize + ")."); } m_records = header->Records; int index, size; index = 0; size = header->Records * header->RecordSize; data = new byte[size]; while (index < size) { index += stream.Read(data, index, size - index); } index = 0; size = header->StringPoolSize; pool = new byte[size]; while (index < size) { index += stream.Read(pool, index, size - index); } } fixed(byte *pdata_ = data) { byte *pdata = pdata_; if (m_records > 0) { uint minId = uint.MaxValue; uint maxId = uint.MinValue; byte *pdata2 = pdata; for (int i = 0; i < m_records; i++) { uint id = *(uint *)pdata2; if (minId > id) { minId = id; } if (maxId < id) { maxId = id; } pdata2 += m_entrySize; } this.Resize(minId, maxId); } fixed(byte *ppool = m_haveString?pool : null) { sbyte * spool = (sbyte *)ppool; int poolLen = pool.Length; StringGetter strGetter = offset => LazyCString.LoadString(spool, poolLen, offset); bool ignoreLazyCStrings = !flags.HasFlag(LoadFlags.LazyCStrings); for (int i = 0; i < m_records; i++) { var entry = (T)m_ctor.Invoke(null); m_loadMethod(pdata, pool, strGetter, entry, ignoreLazyCStrings); uint id = *(uint *)pdata; m_entries[id - m_minId] = entry; pdata += m_entrySize; } } } }
public override unsafe void Load(Stream stream, LoadFlags flags) { GenerateLoadMethod(); byte[] headerBytes; byte[] data; byte[] pool; fixed(byte *headerPtr = headerBytes = new byte[DB2Header.Size]) { if (stream.Read(headerBytes, 0, DB2Header.Size) != DB2Header.Size) { throw new IOException("Failed to read the DB2 header."); } var header = (DB2Header *)headerPtr; if (!flags.HasFlag(LoadFlags.IgnoreWrongFourCC) && header->FourCC != 0x32424457) { throw new ArgumentException("This is not a valid DB2 file."); } if (header->RecordSize != m_entrySize) { throw new ArgumentException("This DB2 file has wrong record size (" + m_entrySize + ", expected is " + header->RecordSize + ")."); } m_records = header->Records; this.Hash = header->Hash; this.Build = header->Build; if (header->Build > 12880) { fixed(byte *extHeaderPtr = headerBytes = new byte[DB2ExtendedHeader.Size]) { if (stream.Read(headerBytes, 0, DB2ExtendedHeader.Size) != DB2ExtendedHeader.Size) { throw new IOException("Failed to read the extended DB2 header."); } var extHeader = (DB2ExtendedHeader *)extHeaderPtr; this.Locale = extHeader->Locale; if (extHeader->MaxId != 0) { int diff; checked { diff = (int)(extHeader->MaxId - extHeader->MinId + 1) * 6; } if (stream.CanSeek) { stream.Seek(diff, SeekOrigin.Current); } else { var dummy = new byte[4096]; while (diff > 0) { diff -= stream.Read(dummy, 0, Math.Min(diff, 4096)); } } } } } int index, size; index = 0; size = header->Records * header->RecordSize; data = new byte[size]; while (index < size) { index += stream.Read(data, index, size - index); } index = 0; size = header->StringPoolSize; pool = new byte[size]; while (index < size) { index += stream.Read(pool, index, size - index); } } fixed(byte *pdata_ = data) { byte *pdata = pdata_; if (m_records > 0) this.Resize(*(uint *)pdata, *(uint *)(pdata + m_entrySize * (m_records - 1))); fixed(byte *ppool = m_haveString?pool : null) { sbyte * spool = (sbyte *)ppool; int poolLen = pool.Length; StringGetter strGetter = offset => LazyCString.LoadString(spool, poolLen, offset); bool ignoreLazyCStrings = !flags.HasFlag(LoadFlags.LazyCStrings); for (int i = 0; i < m_records; i++) { var entry = (T)m_ctor.Invoke(null); m_loadMethod(pdata, pool, strGetter, entry, ignoreLazyCStrings); uint id = *(uint *)pdata; m_entries[id - m_minId] = entry; pdata += m_entrySize; } } } }