2022-02-03 15:26:44 +00:00
|
|
|
namespace ASC.Security.Cryptography;
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
public class MachinePseudoKeys
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
private readonly byte[] _confKey = null;
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
public MachinePseudoKeys(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
var key = configuration["core:machinekey"];
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
if (string.IsNullOrEmpty(key))
|
|
|
|
{
|
|
|
|
key = configuration["asc:common.machinekey"];
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
if (!string.IsNullOrEmpty(key))
|
|
|
|
{
|
|
|
|
_confKey = Encoding.UTF8.GetBytes(key);
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
public byte[] GetMachineConstant()
|
|
|
|
{
|
2022-02-08 11:07:28 +00:00
|
|
|
if (_confKey != null)
|
|
|
|
{
|
|
|
|
return _confKey;
|
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
var path = typeof(MachinePseudoKeys).Assembly.Location;
|
|
|
|
var fi = new FileInfo(path);
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
return BitConverter.GetBytes(fi.CreationTime.ToOADate());
|
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
public byte[] GetMachineConstant(int bytesCount)
|
|
|
|
{
|
|
|
|
var cnst = Enumerable.Repeat<byte>(0, sizeof(int)).Concat(GetMachineConstant()).ToArray();
|
|
|
|
var icnst = BitConverter.ToInt32(cnst, cnst.Length - sizeof(int));
|
|
|
|
var rnd = new AscRandom(icnst);
|
|
|
|
var buff = new byte[bytesCount];
|
|
|
|
rnd.NextBytes(buff);
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
return buff;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
}
|