/* * Copyright (C) 2012-2020 CypherCore * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ namespace System { public static class Extensions { static uint LeftRotate(this uint value, int shiftCount) { return (value << shiftCount) | (value >> (0x20 - shiftCount)); } public static byte[] GenerateRandomKey(this byte[] s, int length) { var random = new Random((int)((uint)(Guid.NewGuid().GetHashCode() ^ 1 >> 89 << 2 ^ 42)).LeftRotate(13)); var key = new byte[length]; for (int i = 0; i < length; i++) { int randValue; do { randValue = (int)((uint)random.Next(0xFF)).LeftRotate(1) ^ i; } while (randValue > 0xFF && randValue <= 0); key[i] = (byte)randValue; } return key; } public static bool Compare(this byte[] b, byte[] b2) { for (int i = 0; i < b2.Length; i++) if (b[i] != b2[i]) return false; return true; } public static byte[] Combine(this byte[] data, params byte[][] pData) { var combined = data; foreach (var arr in pData) { var currentSize = combined.Length; Array.Resize(ref combined, currentSize + arr.Length); Buffer.BlockCopy(arr, 0, combined, currentSize, arr.Length); } return combined; } } }