public void GenerateKey(Int32 keySize) { // 生成密钥 var ks = ECDsaHelper.GenerateKey(keySize); Assert.NotNull(ks); Assert.Equal(2, ks.Length); //var magic = ks[0].ToBase64().ReadBytes(0, 4).ToInt(); //var magic2 = ks[1].ToBase64().ReadBytes(0, 4).ToInt(); { // 重新导入 var data = ks[0].ToBase64(); var key = CngKey.Import(data, CngKeyBlobFormat.EccPrivateBlob); var ec = new ECDsaCng(key); // 解码KeyBlob格式 var eckey = new ECKey(); eckey.Read(data); Assert.Equal(data.ToBase64(), eckey.ToArray().ToBase64()); // 幻数(4) + 长度len(4) + X(len) + Y(len) + D(len) Assert.Equal($"ECDSA_PRIVATE_P{keySize}", eckey.Algorithm); // 构造参数 var ecp = eckey.ExportParameters(); // 再次以参数导入,然后导出key进行对比 var ec2 = new ECDsaCng(); ec2.ImportParameters(ecp); var key2 = ec2.Key.Export(CngKeyBlobFormat.EccPrivateBlob).ToBase64(); Assert.Equal(ks[0], key2); } { // 重新导入 var data = ks[1].ToBase64(); var key = CngKey.Import(data, CngKeyBlobFormat.EccPublicBlob); var ec = new ECDsaCng(key); // 解码KeyBlob格式 var eckey = new ECKey(); eckey.Read(data); Assert.Equal(data.ToBase64(), eckey.ToArray().ToBase64()); // 幻数(4) + 长度len(4) + X(len) + Y(len) + D(len) Assert.Equal($"ECDSA_PUBLIC_P{keySize}", eckey.Algorithm); // 构造参数 var ecp = eckey.ExportParameters(); // 再次以参数导入,然后导出key进行对比 var ec2 = new ECDsaCng(); ec2.ImportParameters(ecp); var key2 = ec2.Key.Export(CngKeyBlobFormat.EccPublicBlob).ToBase64(); Assert.Equal(ks[1], key2); } }
public void Create() { var ks = ECDsaHelper.GenerateKey(); var ec = ECDsaHelper.Create(ks[0]); Assert.NotNull(ec); var ec2 = ECDsaHelper.Create(prvKey); Assert.NotNull(ec2); var ec3 = ECDsaHelper.Create(pubKey); Assert.NotNull(ec3); }
private static void Test1() { var keys = ECDsaHelper.GenerateKey(); XTrace.WriteLine("prvKey:{0}", keys[0]); XTrace.WriteLine("pubKey:{0}", keys[1]); //"你好".SpeakAsync(); XTrace.WriteLine("FullPath:{0}", ".".GetFullPath()); XTrace.WriteLine("BasePath:{0}", ".".GetBasePath()); XTrace.WriteLine("TempPath:{0}", Path.GetTempPath()); var mi = MachineInfo.Current ?? MachineInfo.RegisterAsync().Result; foreach (var pi in mi.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { XTrace.WriteLine("{0}:\t{1}", pi.Name, mi.GetValue(pi)); } Console.WriteLine(); #if __CORE__ foreach (var pi in typeof(RuntimeInformation).GetProperties()) { XTrace.WriteLine("{0}:\t{1}", pi.Name, pi.GetValue(null)); } #endif //Console.WriteLine(); //foreach (var pi in typeof(Environment).GetProperties()) //{ // XTrace.WriteLine("{0}:\t{1}", pi.Name, pi.GetValue(null)); //} mi = MachineInfo.Current; for (var i = 0; i < 100; i++) { XTrace.WriteLine("CPU={0:p2} Temp={1} Memory={2:n0} Disk={3}", mi.CpuRate, mi.Temperature, mi.AvailableMemory.ToGMK(), MachineInfo.GetFreeSpace().ToGMK()); Thread.Sleep(1000); mi.Refresh(); } Console.ReadKey(); }
public void SignAndVerify() { var ks = ECDsaHelper.GenerateKey(256); var data = Rand.NextBytes(1000); { var sign = ECDsaHelper.Sign(data, ks[0]); Assert.NotNull(sign); var rs = ECDsaHelper.Verify(data, ks[1], sign); Assert.True(rs); } { var sign = ECDsaHelper.SignSha256(data, ks[0]); Assert.NotNull(sign); var rs = ECDsaHelper.VerifySha256(data, ks[1], sign); Assert.True(rs); } { var sign = ECDsaHelper.SignSha384(data, ks[0]); Assert.NotNull(sign); var rs = ECDsaHelper.VerifySha384(data, ks[1], sign); Assert.True(rs); } { var sign = ECDsaHelper.SignSha512(data, ks[0]); Assert.NotNull(sign); var rs = ECDsaHelper.VerifySha512(data, ks[1], sign); Assert.True(rs); } }