-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
63 lines (52 loc) · 1.56 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
namespace AnkiEditor
{
/// <summary>
/// Based on https://github.com/dae/anki/blob/master/anki/utils.py
/// Used for GUID generation like ANKI
/// </summary>
public static class Utils
{
private static Random random = new Random();
private static string BaseX(ulong number, char[] baseTable)
{
uint x = (uint)baseTable.Length;
var num = number;
var result = "";
while (num > 0)
{
var i = num % x;
num = num / x;
result = baseTable[i] + result;
}
return result;
}
/// <summary>
/// # all printable characters minus quotes, backslash and separators
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private static string Base91(ulong num)
{
char[] table = new char[91];
int i = 0;
for (char j = '\x21'; j <= '\x7e'; j++)
{
if (j != '\x27' && j != '\x2d' && j != '\x5c')
table[i++] = j;
}
return BaseX(num, table);
}
/// <summary>
/// "Return a base91-encoded 64bit random number."
/// </summary>
/// <returns></returns>
public static string Guid64()
{
byte[] buffer = new byte[8];
random.NextBytes(buffer);
var ulongrand = BitConverter.ToUInt64(buffer, 0);
return Base91(ulongrand);
}
}
}