2019-05-15 14:56:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace ASC.Common.Security
|
|
|
|
{
|
|
|
|
public class AscRandom : Random
|
|
|
|
{
|
2022-01-21 09:27:39 +00:00
|
|
|
private int _inext;
|
|
|
|
private int _inextp;
|
2019-05-15 14:56:09 +00:00
|
|
|
private const int MBIG = int.MaxValue;
|
|
|
|
private const int MSEED = 161803398;
|
|
|
|
private const int MZ = 0;
|
2019-08-15 14:36:08 +00:00
|
|
|
private readonly int[] seeds;
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
public AscRandom() : this(Environment.TickCount)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public AscRandom(int seed)
|
|
|
|
{
|
|
|
|
seeds = new int[56];
|
|
|
|
var num4 = (seed == int.MinValue) ? int.MaxValue : Math.Abs(seed);
|
|
|
|
var num2 = 161803398 - num4;
|
2019-08-15 14:48:55 +00:00
|
|
|
seeds[^1] = num2;
|
2019-05-15 14:56:09 +00:00
|
|
|
var num3 = 1;
|
2019-08-15 13:03:57 +00:00
|
|
|
for (var i = 1; i < seeds.Length - 1; i++)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-01-21 13:46:30 +00:00
|
|
|
var index = 21 * i % (seeds.Length - 1);
|
2019-05-15 14:56:09 +00:00
|
|
|
seeds[index] = num3;
|
|
|
|
num3 = num2 - num3;
|
|
|
|
if (num3 < 0)
|
|
|
|
{
|
|
|
|
num3 += int.MaxValue;
|
|
|
|
}
|
|
|
|
num2 = seeds[index];
|
|
|
|
}
|
2019-08-15 13:03:57 +00:00
|
|
|
for (var j = 1; j < 5; j++)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-15 13:03:57 +00:00
|
|
|
for (var k = 1; k < seeds.Length; k++)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
seeds[k] -= seeds[1 + ((k + 30) % (seeds.Length - 1))];
|
|
|
|
if (seeds[k] < 0)
|
|
|
|
{
|
|
|
|
seeds[k] += int.MaxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 09:27:39 +00:00
|
|
|
_inext = 0;
|
|
|
|
_inextp = 21;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int Next(int maxValue)
|
|
|
|
{
|
|
|
|
if (maxValue < 0)
|
|
|
|
{
|
2019-08-16 10:15:25 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(maxValue));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
return (int)(InternalSample() * 4.6566128752457969E-10 * maxValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void NextBytes(byte[] buffer)
|
|
|
|
{
|
2019-08-16 10:15:25 +00:00
|
|
|
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < buffer.Length; i++)
|
|
|
|
{
|
2019-08-15 13:16:39 +00:00
|
|
|
buffer[i] = (byte)(InternalSample() % (byte.MaxValue + 1));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int InternalSample()
|
|
|
|
{
|
2022-01-21 09:27:39 +00:00
|
|
|
var inext = this._inext;
|
|
|
|
var inextp = this._inextp;
|
2019-05-15 14:56:09 +00:00
|
|
|
if (++inext >= seeds.Length - 1)
|
|
|
|
{
|
|
|
|
inext = 1;
|
|
|
|
}
|
|
|
|
if (++inextp >= seeds.Length - 1)
|
|
|
|
{
|
|
|
|
inextp = 1;
|
|
|
|
}
|
2019-08-15 13:03:57 +00:00
|
|
|
var num = seeds[inext] - seeds[inextp];
|
2019-05-15 14:56:09 +00:00
|
|
|
if (num == int.MaxValue)
|
|
|
|
{
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
if (num < 0)
|
|
|
|
{
|
|
|
|
num += int.MaxValue;
|
|
|
|
}
|
|
|
|
seeds[inext] = num;
|
2022-01-21 09:27:39 +00:00
|
|
|
this._inext = inext;
|
|
|
|
this._inextp = inextp;
|
2019-05-15 14:56:09 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|