-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTool.cs
More file actions
74 lines (65 loc) · 2.44 KB
/
Copy pathSignatureTool.cs
File metadata and controls
74 lines (65 loc) · 2.44 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
72
73
74
#if !UNITY_WSA || UNITY_EDITOR
using System.Security.Cryptography;
#else
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#endif
using System;
using UnityEngine;
namespace Runtime
{
using Runtime.Pattern;
public class SignatureTool : Singleton<SignatureTool>
{
const string PUBLIC_KEY = "BgIAAACkAABSU0ExAAQAAAEAAQA9A4S5rD4yCIzmeBkccQMwK/Sy3RDYdUeukpLuiMeMLNYlQ+jZwUXauxZ6DP8cZ29JlC1DzN9b89WKwzGprdeI6RV+pzQceR1bTdEEgGMQA7fQL+reOdmnBT3B70xRRn9DHufD/zxsOKN+mVoaF5T7VeKOZnse1IISjMgh7RL50w==";
#if !UNITY_WSA || UNITY_EDITOR
RSACryptoServiceProvider rsa;
SHA1 sha;
#else
AsymmetricKeyAlgorithmProvider rsa;
CryptographicKey key;
#endif
public SignatureTool()
{
#if !UNITY_WSA || UNITY_EDITOR
rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(Convert.FromBase64String(PUBLIC_KEY));
sha = new SHA1CryptoServiceProvider();
#else
rsa = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaSignPkcs1Sha1);
key = rsa.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(PUBLIC_KEY), CryptographicPublicKeyBlobType.Capi1PublicKey);
#endif
}
public byte[] LoadAndVerify(byte[] data)
{
if (data == null)
{
return null;
}
if (data.Length < 128)
{
throw new InvalidProgramException("data length less than 128!");
}
byte[] sig = new byte[128];
byte[] filecontent = new byte[data.Length - 128];
Array.Copy(data, sig, 128);
Array.Copy(data, 128, filecontent, 0, filecontent.Length);
#if !UNITY_WSA || UNITY_EDITOR
if (!rsa.VerifyData(filecontent, sha, sig))
{
throw new InvalidProgramException("data has invalid signature!");
}
#else
if (!CryptographicEngine.VerifySignature(key, CryptographicBuffer.CreateFromByteArray(filecontent), CryptographicBuffer.CreateFromByteArray(sig)))
{
throw new InvalidProgramException("data has invalid signature!");
}
#endif
return filecontent;
}
public string GetText(byte[] data)
{
return System.Text.Encoding.UTF8.GetString(LoadAndVerify(data));
}
}
}