-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathKeyGenerator.cs
More file actions
71 lines (61 loc) · 2.07 KB
/
Copy pathKeyGenerator.cs
File metadata and controls
71 lines (61 loc) · 2.07 KB
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
64
65
66
67
68
69
70
71
using System.Text;
using System.Security.Cryptography;
using System;
using UnityEngine;
public class KeyGenerator
{
//key1 is fixed key
//key2 is user key
public static byte[] MakeKeyBytes(string fixedKey, string userKey, int userKeylength = 4)
{
byte[] key = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] fixedKeyBytes = Encoding.ASCII.GetBytes(fixedKey);
byte[] userKeyBytes = Encoding.ASCII.GetBytes(userKey);
byte[] hash = GetKeyHash(userKeyBytes);
for (int i = 0; i < fixedKeyBytes.Length; ++i)
key[i] = fixedKeyBytes[i];
if (userKeylength > 0)
{
for (int i = (16 - userKeylength), j = 0; i < key.Length; ++i, ++j)
{
if (j < userKeyBytes.Length)
key[i] = userKeyBytes[j] ^= hash[j];
else
key[i] = hash[j];
}
}
return key;
}
public static byte[] GetKeyHash(byte[] key, string salt = null)
{
SHA256 sha256 = SHA256.Create();
byte[] hash = sha256.ComputeHash(key);
if (salt == null)
{
return hash;
}
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
for(int i = 0; i < hash.Length; ++i)
hash[i] ^= saltBytes[i % saltBytes.Length];
return hash;
}
public static byte[] GetHash(int data)
{
SHA256 sha256 = SHA256.Create();
byte[] bytes = BitConverter.GetBytes(data);
byte[] hash = sha256.ComputeHash(bytes);
return hash;
}
public static string GenerateRandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=|\\/?.>,<~`\'\" ";
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
for (int i = 0; i < length; i++)
{
int index = random.Next(chars.Length);
builder.Append(chars[index]);
}
return builder.ToString();
}
}