private static RsaSecurityKey Create() { RsaSecurityKey key; var rsa = RSA.Create(); if (rsa is RSACryptoServiceProvider) { rsa.Dispose(); using (var cng = new RSACng(2048)) { var parameters = cng.ExportParameters(includePrivateParameters: true); key = new RsaSecurityKey(parameters); } } else { rsa.KeySize = 2048; key = new RsaSecurityKey(rsa); } var buffer = RandomUtility.GenerateByteArray(16); key.KeyId = buffer.AsBase64String(); return(key); }
/// <summary> /// 生成标识核心。 /// </summary> /// <param name="timestampBytes">给定的时间戳字节数组。</param> /// <returns>返回 <see cref="Guid"/>。</returns> protected virtual Guid GenerateIdCore(byte[] timestampBytes) { return(ExtensionSettings.Preference.RunLocker(() => { var randomBytes = RandomUtility.GenerateByteArray(10); var guidBytes = new byte[16]; switch (Generation) { case CombIdentificationGeneration.AsString: case CombIdentificationGeneration.AsBinary: Buffer.BlockCopy(timestampBytes, 2, guidBytes, 0, 6); Buffer.BlockCopy(randomBytes, 0, guidBytes, 6, 10); // If formatting as a string, we have to reverse the order // of the Data1 and Data2 blocks on little-endian systems. if (Generation == CombIdentificationGeneration.AsString && BitConverter.IsLittleEndian) { Array.Reverse(guidBytes, 0, 4); Array.Reverse(guidBytes, 4, 2); } break; case CombIdentificationGeneration.AtEnd: Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10); Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6); break; } return new Guid(guidBytes); })); }
public void GenerateByteArrayTest() { var buffer = RandomUtility.GenerateByteArray(16); Assert.NotEmpty(buffer); var g = new Guid(buffer); Assert.NotEmpty(g.ToString()); }
private Task <string> GenerateTokenAsync(string idTraceName, CancellationToken cancellationToken) { return(cancellationToken.RunOrCancelAsync(() => { var buffer = RandomUtility.GenerateByteArray(32); var token = buffer.AsBase64String(); Logger.LogTrace($"Generate {idTraceName}: {token}"); return token; })); }