Ejemplo n.º 1
0
        public static int VerifyAddress(String address)
        {
            if (!address.StartsWith("1") && !address.StartsWith("WX") && !address.StartsWith("WR"))
            {
                return(-1);
            }
            byte[] r5;
            if (address.StartsWith("1"))
            {
                r5 = Base58Check.Decode(address);
            }
            else
            {
                r5 = Base58Check.Decode(address.Substring(2));
            }
            Sha3Keccack sha3Keccack = Sha3Keccack.Current;

            byte[] r3  = sha3Keccack.CalculateHash(sha3Keccack.CalculateHash(AddressToPubkeyHashByteArray(address)));
            byte[] b4  = Utils.CopyByteArray(r3, 0, 4);
            byte[] _b4 = Utils.CopyByteArray(r5, r5.Length - 4, 4);
            if (Array.Equals(b4, _b4))
            {
                return(0);
            }
            else
            {
                return(-2);
            }
        }
Ejemplo n.º 2
0
        public static string PubkeyHashToAddress(byte[] publicHash)
        {
            Sha3Keccack sha3Keccack = Sha3Keccack.Current;

            byte[] r1 = publicHash;
            byte[] r2 = Utils.prepend(r1, (byte)0x00);
            byte[] r3 = sha3Keccack.CalculateHash(sha3Keccack.CalculateHash(r1));
            byte[] b4 = Utils.CopyByteArray(r3, 0, 4);
            byte[] b5 = Utils.Combine(r2, b4);
            string s6 = "WX" + Base58Check.Encode(b5);

            return(s6);
        }
Ejemplo n.º 3
0
        public static string PubkeyToAddress(byte[] pubkey)
        {
            Sha3Keccack sha3Keccack = Sha3Keccack.Current;

            byte[] pub256 = sha3Keccack.CalculateHash(pubkey);
            byte[] r1     = RipemdManager.getHash(pub256);
            return(PubkeyHashToAddress(r1));
        }
Ejemplo n.º 4
0
        public static bool verifyPassword(Keystore keystore, string password)
        {
            byte[]      derivedKey   = Argon2Manager.Current.hash(System.Text.Encoding.ASCII.GetBytes(password), keystore.kdfparams.salt.HexToByteArray());
            byte[]      cipherPriKey = keystore.crypto.ciphertext.HexToByteArray();
            Sha3Keccack sha3Keccack  = Sha3Keccack.Current;

            byte[] mac = sha3Keccack.CalculateHash(Utils.Combine(derivedKey, cipherPriKey));
            return(mac.ToHex().Equals(keystore.mac));
        }
Ejemplo n.º 5
0
 public static string FromPassword(string password)
 {
     if (password.Length > 20 || password.Length < 8)
     {
         return(JsonConvert.SerializeObject(""));
     }
     else
     {
         KeyPair   keyPair   = KeyPair.generateEd25519KeyPair();
         PublicKey publicKey = keyPair.GetPublicKey();
         byte[]    salt      = new byte[saltLength];
         byte[]    iv        = new byte[ivLength];
         Org.BouncyCastle.Security.SecureRandom random = new Org.BouncyCastle.Security.SecureRandom();
         random.NextBytes(iv);
         Org.BouncyCastle.Security.SecureRandom sr = new Org.BouncyCastle.Security.SecureRandom();
         sr.NextBytes(salt);
         Argon2Manager argon2Manager = Argon2Manager.Current;
         byte[]        derivedKey    = argon2Manager.hash(System.Text.Encoding.ASCII.GetBytes(password), salt);
         AesManager    aes           = AesManager.Current;
         byte[]        cipherPrivKey = aes.Encryptdata(keyPair.GetPrivateKey().getBytes(), derivedKey, iv);
         Sha3Keccack   sha3Keccack   = Sha3Keccack.Current;
         byte[]        mac           = sha3Keccack.CalculateHash(Utils.Combine(derivedKey, cipherPrivKey));
         Crypto        crypto        = new Crypto(
             AesManager.cipher, new string(cipherPrivKey.ToHex()),
             new Cipherparams(
                 new string(iv.ToHex())
                 )
             );
         Kdfparams kdfparams = new Kdfparams(Argon2Manager.memoryCost, Argon2Manager.timeCost, Argon2Manager.parallelism, new string(salt.ToHex()));
         Address   address   = new Address(publicKey);
         Keystore  ks        = new Keystore(address.getAddress(), crypto, Utils.generateUUID(),
                                            newVersion, new string(mac.ToHex()), argon2Manager.kdf(), kdfparams
                                            );
         return(JsonConvert.SerializeObject(ks));
     }
 }
Ejemplo n.º 6
0
        public void TestSha3KeccackWithEverything()
        {
            Sha3Keccack sha3Keccack = Sha3Keccack.Current;

            Assert.Equal("b8f132fb6526e0405f3ce4f3bab301f1d4409b1e7f2c01c2037d6cf845c831cb", sha3Keccack.CalculateHash("LogCreate(address)"));
        }