2017-05-11 17:41:13 +02:00
|
|
|
|
using Bit.Core.Models.Data;
|
2017-07-10 20:30:12 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2017-03-21 05:04:39 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
2017-08-11 23:06:31 +02:00
|
|
|
|
using System.IO;
|
2017-07-28 06:17:31 +02:00
|
|
|
|
using System.Linq;
|
2017-08-11 23:06:31 +02:00
|
|
|
|
using System.Reflection;
|
2017-06-29 21:55:39 +02:00
|
|
|
|
using System.Security.Cryptography;
|
2017-01-13 00:35:26 +01:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2017-06-29 21:55:39 +02:00
|
|
|
|
using System.Text;
|
2017-01-13 00:35:26 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-08-22 18:38:48 +02:00
|
|
|
|
using Dapper;
|
2016-05-21 23:16:22 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static class CoreHelpers
|
|
|
|
|
{
|
|
|
|
|
private static readonly long _baseDateTicks = new DateTime(1900, 1, 1).Ticks;
|
2017-07-14 15:05:15 +02:00
|
|
|
|
private static readonly DateTime _epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
2017-07-28 06:17:31 +02:00
|
|
|
|
private static readonly Random _random = new Random();
|
2017-08-25 14:57:43 +02:00
|
|
|
|
private static string _version;
|
|
|
|
|
private static int _versionWeight;
|
2016-05-21 23:16:22 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generate sequential Guid for Sql Server.
|
|
|
|
|
/// ref: https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Id/GuidCombGenerator.cs
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A comb Guid.</returns>
|
|
|
|
|
public static Guid GenerateComb()
|
|
|
|
|
{
|
|
|
|
|
var guidArray = Guid.NewGuid().ToByteArray();
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
// Get the days and milliseconds which will be used to build the byte string
|
|
|
|
|
var days = new TimeSpan(now.Ticks - _baseDateTicks);
|
|
|
|
|
var msecs = now.TimeOfDay;
|
|
|
|
|
|
|
|
|
|
// Convert to a byte array
|
|
|
|
|
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
|
|
|
|
|
var daysArray = BitConverter.GetBytes(days.Days);
|
|
|
|
|
var msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
|
|
|
|
|
|
|
|
|
|
// Reverse the bytes to match SQL Servers ordering
|
|
|
|
|
Array.Reverse(daysArray);
|
|
|
|
|
Array.Reverse(msecsArray);
|
|
|
|
|
|
|
|
|
|
// Copy the bytes into the guid
|
|
|
|
|
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
|
|
|
|
|
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
|
|
|
|
|
|
|
|
|
|
return new Guid(guidArray);
|
|
|
|
|
}
|
2017-01-13 00:35:26 +01:00
|
|
|
|
|
2017-03-21 05:04:39 +01:00
|
|
|
|
public static DataTable ToGuidIdArrayTVP(this IEnumerable<Guid> ids)
|
|
|
|
|
{
|
|
|
|
|
return ids.ToArrayTVP("GuidId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DataTable ToArrayTVP<T>(this IEnumerable<T> values, string columnName)
|
|
|
|
|
{
|
2017-08-09 14:14:45 +02:00
|
|
|
|
var table = new DataTable();
|
2017-08-22 18:38:48 +02:00
|
|
|
|
table.SetTypeName($"[dbo].[{columnName}Array]");
|
2017-03-21 05:04:39 +01:00
|
|
|
|
table.Columns.Add(columnName, typeof(T));
|
|
|
|
|
|
|
|
|
|
if(values != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(var value in values)
|
|
|
|
|
{
|
|
|
|
|
table.Rows.Add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 17:41:13 +02:00
|
|
|
|
public static DataTable ToArrayTVP(this IEnumerable<SelectionReadOnly> values)
|
|
|
|
|
{
|
2017-08-09 14:14:45 +02:00
|
|
|
|
var table = new DataTable();
|
2017-08-22 18:38:48 +02:00
|
|
|
|
table.SetTypeName("[dbo].[SelectionReadOnlyArray]");
|
2017-05-11 17:41:13 +02:00
|
|
|
|
|
|
|
|
|
var idColumn = new DataColumn("Id", typeof(Guid));
|
|
|
|
|
table.Columns.Add(idColumn);
|
|
|
|
|
var readOnlyColumn = new DataColumn("ReadOnly", typeof(bool));
|
|
|
|
|
table.Columns.Add(readOnlyColumn);
|
|
|
|
|
|
|
|
|
|
if(values != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(var value in values)
|
|
|
|
|
{
|
|
|
|
|
var row = table.NewRow();
|
|
|
|
|
row[idColumn] = value.Id;
|
|
|
|
|
row[readOnlyColumn] = value.ReadOnly;
|
|
|
|
|
table.Rows.Add(row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 23:06:31 +02:00
|
|
|
|
public static string CleanCertificateThumbprint(string thumbprint)
|
2017-01-13 00:35:26 +01:00
|
|
|
|
{
|
|
|
|
|
// Clean possible garbage characters from thumbprint copy/paste
|
|
|
|
|
// ref http://stackoverflow.com/questions/8448147/problems-with-x509store-certificates-find-findbythumbprint
|
2017-08-11 23:06:31 +02:00
|
|
|
|
return Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static X509Certificate2 GetCertificate(string thumbprint)
|
|
|
|
|
{
|
|
|
|
|
thumbprint = CleanCertificateThumbprint(thumbprint);
|
2017-01-13 00:35:26 +01:00
|
|
|
|
|
|
|
|
|
X509Certificate2 cert = null;
|
|
|
|
|
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
|
|
|
|
|
certStore.Open(OpenFlags.ReadOnly);
|
|
|
|
|
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
|
|
|
|
|
if(certCollection.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
cert = certCollection[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
certStore.Close();
|
|
|
|
|
return cert;
|
|
|
|
|
}
|
2017-01-14 18:47:44 +01:00
|
|
|
|
|
2017-08-07 17:24:16 +02:00
|
|
|
|
public static X509Certificate2 GetCertificate(string file, string password)
|
|
|
|
|
{
|
|
|
|
|
return new X509Certificate2(file, password);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 23:06:31 +02:00
|
|
|
|
public static X509Certificate2 GetEmbeddedCertificate(string file, string password)
|
|
|
|
|
{
|
|
|
|
|
var assembly = typeof(CoreHelpers).GetTypeInfo().Assembly;
|
|
|
|
|
using(var s = assembly.GetManifestResourceStream($"Bit.Core.{file}"))
|
|
|
|
|
using(var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
s.CopyTo(ms);
|
|
|
|
|
return new X509Certificate2(ms.ToArray(), password);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 05:17:34 +01:00
|
|
|
|
public static long ToEpocMilliseconds(DateTime date)
|
2017-01-14 18:47:44 +01:00
|
|
|
|
{
|
|
|
|
|
return (long)Math.Round((date - _epoc).TotalMilliseconds, 0);
|
|
|
|
|
}
|
2017-03-23 05:17:34 +01:00
|
|
|
|
|
|
|
|
|
public static DateTime FromEpocMilliseconds(long milliseconds)
|
|
|
|
|
{
|
|
|
|
|
return _epoc.AddMilliseconds(milliseconds);
|
|
|
|
|
}
|
2017-06-22 23:03:35 +02:00
|
|
|
|
|
2017-08-12 04:55:25 +02:00
|
|
|
|
public static long ToEpocSeconds(DateTime date)
|
|
|
|
|
{
|
|
|
|
|
return (long)Math.Round((date - _epoc).TotalSeconds, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime FromEpocSeconds(long seconds)
|
|
|
|
|
{
|
|
|
|
|
return _epoc.AddSeconds(seconds);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 23:03:35 +02:00
|
|
|
|
public static string U2fAppIdUrl(GlobalSettings globalSettings)
|
|
|
|
|
{
|
2017-08-04 05:12:05 +02:00
|
|
|
|
return string.Concat(globalSettings.BaseServiceUri.Vault, "/app-id.json");
|
2017-06-22 23:03:35 +02:00
|
|
|
|
}
|
2017-06-29 21:55:39 +02:00
|
|
|
|
|
2017-07-28 06:17:31 +02:00
|
|
|
|
public static string RandomString(int length, bool alpha = true, bool upper = true, bool lower = true,
|
2017-06-29 21:55:39 +02:00
|
|
|
|
bool numeric = true, bool special = false)
|
|
|
|
|
{
|
2017-07-28 06:17:31 +02:00
|
|
|
|
return RandomString(length, RandomStringCharacters(alpha, upper, lower, numeric, special));
|
|
|
|
|
}
|
2017-06-29 21:55:39 +02:00
|
|
|
|
|
2017-07-28 06:17:31 +02:00
|
|
|
|
public static string RandomString(int length, string characters)
|
|
|
|
|
{
|
|
|
|
|
return new string(Enumerable.Repeat(characters, length).Select(s => s[_random.Next(s.Length)]).ToArray());
|
|
|
|
|
}
|
2017-06-29 21:55:39 +02:00
|
|
|
|
|
2017-07-28 06:17:31 +02:00
|
|
|
|
public static string SecureRandomString(int length, bool alpha = true, bool upper = true, bool lower = true,
|
|
|
|
|
bool numeric = true, bool special = false)
|
|
|
|
|
{
|
|
|
|
|
return SecureRandomString(length, RandomStringCharacters(alpha, upper, lower, numeric, special));
|
2017-06-29 21:55:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ref https://stackoverflow.com/a/8996788/1090359 with modifications
|
|
|
|
|
public static string SecureRandomString(int length, string characters)
|
|
|
|
|
{
|
|
|
|
|
if(length < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((characters?.Length ?? 0) == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(characters), "characters invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int byteSize = 0x100;
|
|
|
|
|
if(byteSize < characters.Length)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
string.Format("{0} may contain no more than {1} characters.", nameof(characters), byteSize),
|
|
|
|
|
nameof(characters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outOfRangeStart = byteSize - (byteSize % characters.Length);
|
|
|
|
|
using(var rng = RandomNumberGenerator.Create())
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
var buffer = new byte[128];
|
|
|
|
|
while(sb.Length < length)
|
|
|
|
|
{
|
|
|
|
|
rng.GetBytes(buffer);
|
|
|
|
|
for(var i = 0; i < buffer.Length && sb.Length < length; ++i)
|
|
|
|
|
{
|
|
|
|
|
// Divide the byte into charSet-sized groups. If the random value falls into the last group and the
|
|
|
|
|
// last group is too small to choose from the entire allowedCharSet, ignore the value in order to
|
|
|
|
|
// avoid biasing the result.
|
|
|
|
|
if(outOfRangeStart <= buffer[i])
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append(characters[buffer[i] % characters.Length]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-01 05:01:41 +02:00
|
|
|
|
|
2017-07-28 06:17:31 +02:00
|
|
|
|
private static string RandomStringCharacters(bool alpha, bool upper, bool lower, bool numeric, bool special)
|
|
|
|
|
{
|
|
|
|
|
var characters = string.Empty;
|
|
|
|
|
if(alpha)
|
|
|
|
|
{
|
|
|
|
|
if(upper)
|
|
|
|
|
{
|
|
|
|
|
characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(lower)
|
|
|
|
|
{
|
|
|
|
|
characters += "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(numeric)
|
|
|
|
|
{
|
|
|
|
|
characters += "0123456789";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(special)
|
|
|
|
|
{
|
|
|
|
|
characters += "!@#$%^*&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return characters;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 05:01:41 +02:00
|
|
|
|
// ref: https://stackoverflow.com/a/11124118/1090359
|
|
|
|
|
// Returns the human-readable file size for an arbitrary 64-bit file size .
|
|
|
|
|
// The format is "0.## XB", ex: "4.2 KB" or "1.43 GB"
|
|
|
|
|
public static string ReadableBytesSize(long size)
|
|
|
|
|
{
|
|
|
|
|
// Get absolute value
|
|
|
|
|
var absoluteSize = (size < 0 ? -size : size);
|
|
|
|
|
|
|
|
|
|
// Determine the suffix and readable value
|
|
|
|
|
string suffix;
|
|
|
|
|
double readable;
|
|
|
|
|
if(absoluteSize >= 0x40000000) // 1 Gigabyte
|
|
|
|
|
{
|
|
|
|
|
suffix = "GB";
|
|
|
|
|
readable = (size >> 20);
|
|
|
|
|
}
|
|
|
|
|
else if(absoluteSize >= 0x100000) // 1 Megabyte
|
|
|
|
|
{
|
|
|
|
|
suffix = "MB";
|
|
|
|
|
readable = (size >> 10);
|
|
|
|
|
}
|
|
|
|
|
else if(absoluteSize >= 0x400) // 1 Kilobyte
|
|
|
|
|
{
|
|
|
|
|
suffix = "KB";
|
|
|
|
|
readable = size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return absoluteSize.ToString("0 Bytes"); // Byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Divide by 1024 to get fractional value
|
|
|
|
|
readable = (readable / 1024);
|
|
|
|
|
|
|
|
|
|
// Return formatted number with suffix
|
|
|
|
|
return readable.ToString("0.## ") + suffix;
|
|
|
|
|
}
|
2017-07-10 20:30:12 +02:00
|
|
|
|
|
|
|
|
|
public static T CloneObject<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj));
|
|
|
|
|
}
|
2017-08-08 23:27:01 +02:00
|
|
|
|
|
|
|
|
|
public static bool FullFramework()
|
|
|
|
|
{
|
|
|
|
|
#if NET461
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool SettingHasValue(string setting)
|
|
|
|
|
{
|
2017-08-09 05:06:28 +02:00
|
|
|
|
if(string.IsNullOrWhiteSpace(setting) || setting.Equals("SECRET") || setting.Equals("REPLACE"))
|
2017-08-08 23:27:01 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-08-11 16:04:59 +02:00
|
|
|
|
|
|
|
|
|
public static string Base64UrlEncode(byte[] input)
|
|
|
|
|
{
|
|
|
|
|
var output = Convert.ToBase64String(input)
|
|
|
|
|
.Replace('+', '-')
|
|
|
|
|
.Replace('/', '_')
|
|
|
|
|
.Replace("=", string.Empty);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] Base64UrlDecode(string input)
|
|
|
|
|
{
|
|
|
|
|
var output = input;
|
|
|
|
|
// 62nd char of encoding
|
|
|
|
|
output = output.Replace('-', '+');
|
|
|
|
|
// 63rd char of encoding
|
|
|
|
|
output = output.Replace('_', '/');
|
|
|
|
|
// Pad with trailing '='s
|
|
|
|
|
switch(output.Length % 4)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
// No pad chars in this case
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
// Two pad chars
|
|
|
|
|
output += "=="; break;
|
|
|
|
|
case 3:
|
|
|
|
|
// One pad char
|
|
|
|
|
output += "="; break;
|
|
|
|
|
default:
|
|
|
|
|
throw new InvalidOperationException("Illegal base64url string!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Standard base64 decoder
|
|
|
|
|
return Convert.FromBase64String(output);
|
|
|
|
|
}
|
2017-08-16 19:55:01 +02:00
|
|
|
|
|
|
|
|
|
public static string FormatLicenseSignatureValue(object val)
|
|
|
|
|
{
|
|
|
|
|
if(val == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(val.GetType() == typeof(DateTime))
|
|
|
|
|
{
|
|
|
|
|
return ToEpocSeconds((DateTime)val).ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(val.GetType() == typeof(bool))
|
|
|
|
|
{
|
|
|
|
|
return val.ToString().ToLowerInvariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return val.ToString();
|
|
|
|
|
}
|
2017-08-25 14:57:43 +02:00
|
|
|
|
|
2017-09-07 05:57:14 +02:00
|
|
|
|
public static string GetVersion()
|
2017-08-25 14:57:43 +02:00
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(_version))
|
|
|
|
|
{
|
|
|
|
|
_version = Assembly.GetEntryAssembly()
|
|
|
|
|
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
|
|
|
|
|
.InformationalVersion;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 05:57:14 +02:00
|
|
|
|
return _version;
|
2017-08-25 14:57:43 +02:00
|
|
|
|
}
|
2016-05-21 23:16:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|