markus / MarkusAutoUpdate / src / NetSparkle.Tests / Ed25519Tests.cs @ 5b48dae7
이력 | 보기 | 이력해설 | 다운로드 (1.64 KB)
1 | d8f5045e | taeseongkim | using NetSparkleUpdater.SignatureVerifiers; |
---|---|---|---|
2 | using Org.BouncyCastle.Crypto; |
||
3 | using Org.BouncyCastle.Crypto.Generators; |
||
4 | using Org.BouncyCastle.Crypto.Parameters; |
||
5 | using Org.BouncyCastle.Crypto.Signers; |
||
6 | using Org.BouncyCastle.Security; |
||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Text; |
||
10 | using Xunit; |
||
11 | |||
12 | namespace NetSparkleUnitTests |
||
13 | { |
||
14 | public class Ed25519Tests |
||
15 | { |
||
16 | private static readonly SecureRandom Random = new SecureRandom(); |
||
17 | |||
18 | [Fact] |
||
19 | public void CanValidateSignature() |
||
20 | { |
||
21 | Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator(); |
||
22 | kpg.Init(new Ed25519KeyGenerationParameters(Random)); |
||
23 | |||
24 | AsymmetricCipherKeyPair kp = kpg.GenerateKeyPair(); |
||
25 | Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters)kp.Private; |
||
26 | Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters)kp.Public; |
||
27 | var pubKeyBase64 = Convert.ToBase64String(publicKey.GetEncoded()); |
||
28 | // create signature for item |
||
29 | byte[] msg = new byte[Random.NextInt() & 255]; |
||
30 | Random.NextBytes(msg); |
||
31 | var signer = new Ed25519Signer(); |
||
32 | signer.Init(true, privateKey); |
||
33 | signer.BlockUpdate(msg, 0, msg.Length); |
||
34 | byte[] signature = signer.GenerateSignature(); |
||
35 | var signatureForAppCast = Convert.ToBase64String(signature); |
||
36 | |||
37 | // verify signature |
||
38 | var checker = new Ed25519Checker(NetSparkleUpdater.Enums.SecurityMode.Strict, pubKeyBase64); |
||
39 | Assert.True(checker.VerifySignature(signatureForAppCast, msg) == NetSparkleUpdater.Enums.ValidationResult.Valid); |
||
40 | } |
||
41 | } |
||
42 | } |