Onionr/pow-csharp/onionrpow/OnionrPow.cs

125 lines
4.7 KiB
C#
Raw Normal View History

2020-09-19 21:01:22 +00:00
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
2020-09-23 21:21:43 +00:00
using System.Security.Cryptography;
2020-09-21 07:10:10 +00:00
using Newtonsoft.Json;
using SHA3;
2020-09-19 21:01:22 +00:00
namespace onionrpow
{
2020-09-21 07:10:10 +00:00
public class Meta{
public string ch { get; set; }
public string type { get; set; }
}
public class Block{
public string meta { get; set; }
public string sig { get; set; }
public string signer { get; set; }
2020-09-23 00:36:08 +00:00
public int n;
public int c;
public int time;
2020-09-21 07:10:10 +00:00
2020-09-23 00:36:08 +00:00
//public List<byte> data { get; set; }
2020-09-21 07:10:10 +00:00
}
2020-09-19 21:01:22 +00:00
public class OnionrPow
{
public static void compute(byte[] data, int difficulty){
using (var shaAlg = SHA3.Net.Sha3.Sha3256())
2020-09-23 21:21:43 +00:00
//using (SHA256 shaAlg = SHA256.Create())
2020-09-19 21:01:22 +00:00
{
2020-09-21 07:10:10 +00:00
string stringData = Encoding.UTF8.GetString(data);
bool found = false;
var justData = new List<byte>();
var metadataJson = new List<byte>();
2020-09-19 21:01:22 +00:00
int counter = 0;
2020-09-21 07:10:10 +00:00
foreach(char c in stringData){
if (found){
justData.Add((byte) c);
}
else if (c == '\n'){
for (int i = 0; i < counter + 1; i++){
metadataJson.Add(data[i]);
}
found = true;
}
else{
//Console.WriteLine(c.ToString());
2020-09-19 21:01:22 +00:00
}
counter += 1;
}
2020-09-21 07:10:10 +00:00
Block block = JsonConvert.DeserializeObject<Block>(Encoding.UTF8.GetString(metadataJson.ToArray()));
2020-09-23 00:36:08 +00:00
block.n = new Random().Next(10000);
block.c = 0;
2020-09-23 21:21:43 +00:00
metadataJson.Clear();
metadataJson.AddRange(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(block)));
int location = Encoding.UTF8.GetString(metadataJson.ToArray()).IndexOf("\"c\":");
2020-09-23 00:36:08 +00:00
2020-09-23 21:21:43 +00:00
var metadata1 = new List<byte>();
var metadata2 = new List<byte>();
var countKey = new List<byte>();
countKey.AddRange(Encoding.UTF8.GetBytes("\"c\":"));
2020-09-23 00:36:08 +00:00
2020-09-23 21:21:43 +00:00
bool afterNum = false;
for (int i = location + 4; i < metadataJson.Count; i++){
if (!afterNum && ((char) metadataJson[i]).Equals(',')){
afterNum = true;
continue;
}
if (afterNum){
metadata2.Add(metadataJson[i]);
}
}
for (int i = 0; i < location; i++){
metadata1.Add(metadataJson[i]);
}
2020-09-19 21:01:22 +00:00
2020-09-23 21:21:43 +00:00
var preCompiled = new List<byte>();
preCompiled.AddRange(metadata1);
preCompiled.AddRange(countKey);
int powCounter = 0;
2020-09-23 00:36:08 +00:00
2020-09-23 21:21:43 +00:00
var justDataArray = justData.ToArray();
justData.Clear();
int difficultyCounter = 0;
while(true){
var compiled = preCompiled.ToList();
compiled.AddRange(metadata1);
compiled.AddRange(Encoding.UTF8.GetBytes(powCounter.ToString()));
compiled.AddRange(metadata2);
compiled.AddRange(justDataArray);
var hash = shaAlg.ComputeHash(compiled.ToArray());
foreach (byte b in hash){
if (b == 0){
difficultyCounter += 1;
if (difficultyCounter == difficulty){
Console.WriteLine(powCounter);
Console.WriteLine(BitConverter.ToString(hash));
2020-09-23 00:36:08 +00:00
goto powDone;
}
2020-09-23 21:21:43 +00:00
continue;
2020-09-23 00:36:08 +00:00
}
2020-09-23 21:21:43 +00:00
difficultyCounter = 0;
break;
2020-09-23 00:36:08 +00:00
}
2020-09-23 21:21:43 +00:00
//Console.WriteLine(powCounter);
powCounter += 1;
2020-09-23 00:36:08 +00:00
}
2020-09-23 21:21:43 +00:00
//Console.WriteLine(location);
//Console.WriteLine(Encoding.UTF8.GetString(metadataJson.ToArray()));
2020-09-19 21:01:22 +00:00
}
2020-09-23 00:36:08 +00:00
powDone:;
2020-09-19 21:01:22 +00:00
}
//b'{"meta":"{\\"ch\\":\\"global\\",\\"type\\":\\"brd\\"}","sig":"pR4qmKGGCdnyNyZRlhGfF9GC7bONCsEnY04lTfiVuTHexPJypOqmxe9iyDQQqdR+PB2gwWuNqGMs5O8\\/S\\/hsCA==","signer":"UO74AP5LGQFI7EJTN6NAVINIPU2XO2KA7CAS6KSWGWAY5XIB5SUA====","time":1600542238,"pow":300182}\nxcvxcvvxcxcv'
}
}