public static string Sha1(byte[] data) { var hash = SHA1.Create().ComputeHash(data); var result = Base58CheckEncoding.Encode(hash); return(result); }
override public string ToString() { byte[] keyBody = new byte[PublicIdentityKey.PREFIX.Length + Bytes.Length]; System.Buffer.BlockCopy(PublicIdentityKey.PREFIX, 0, keyBody, 0, PublicIdentityKey.PREFIX.Length); System.Buffer.BlockCopy(Bytes, 0, keyBody, PublicIdentityKey.PREFIX.Length, Bytes.Length); return(Base58CheckEncoding.Encode(keyBody)); }
override public string ToString() { byte[] withPrefix = new byte[PrivateIdentityKey.PREFIX.Length + Seed.Length]; System.Buffer.BlockCopy(PrivateIdentityKey.PREFIX, 0, withPrefix, 0, PrivateIdentityKey.PREFIX.Length); System.Buffer.BlockCopy(Seed, 0, withPrefix, PrivateIdentityKey.PREFIX.Length, Seed.Length); return(Base58CheckEncoding.Encode(withPrefix)); }
/// <summary> /// Calculates Bitcoin P2PKH address given public key hash and network version bytes /// </summary> /// <param name="hash">public key hash</param> /// <param name="versionBytes">version bytes of network</param> /// <returns>base58check address</returns> public static string ToP2PKHAddress(this Hash160 hash, Span <byte> versionBytes) { Span <byte> data = new byte[versionBytes.Length + hash.Bytes.Length]; versionBytes.CopyTo(data); hash.Bytes.CopyTo(data.Slice(versionBytes.Length, hash.Bytes.Length)); return(Base58CheckEncoding.Encode(data)); }
public void TestEncoding() { // Example address from https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses var addressBytes = "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31".HexToBytes(); const string base58Expected = "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"; var base58Actual = Base58CheckEncoding.Encode(addressBytes); base58Actual.Should().BeEquivalentTo(base58Expected); }
public void TestDecode() { byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); string header = "header-part "; string footer = " footer part."; string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer; SimplePack packer = new SimplePack(header, footer); Assert.AreEqual(packer.decode(encoded), expected_result); }
private static String GetAddressFromHash160(byte[] scriptBytes, int publicKeyHashIndex, byte[] type) { // build up an address byte[] pubKeyHashWithVersion = new byte[2 + 20]; pubKeyHashWithVersion[0] = type[0]; pubKeyHashWithVersion[1] = type[1]; Array.Copy(scriptBytes, publicKeyHashIndex, pubKeyHashWithVersion, 2, 20); return(Base58CheckEncoding.Encode(pubKeyHashWithVersion)); }
public void TestEncode() { byte[] message = UTF8Encoding.UTF8.GetBytes("hello world"); string expected_header = "header-part "; string expected_footer = " footer part."; string expected_result = expected_header + Base58CheckEncoding.Encode(message) + expected_footer; SimplePack packer = new SimplePack(expected_header, expected_footer); string actual_result = packer.encode(message); Assert.AreEqual(actual_result, expected_result); }
public string GetFormatted(string addressPrefix, int chainId) { if (_formatted != null) { return(_formatted); } var addressSuffix = Base58CheckEncoding.Encode(chainId.ToBytes(false)); _formatted = $"{addressPrefix}_{Address.ToBase58()}_{addressSuffix}"; return(_formatted); }
/// <summary> /// Encode a publicKey into CurveLock format. /// </summary> /// <param name="publicKey">A 32 byte publicKey.</param> /// <exception cref="KeyOutOfRangeException"></exception> /// <returns>A Base58 encoded publicKey.</returns> public static string EncodeCurveLockPublicKey(byte[] publicKey) { if (publicKey == null || publicKey.Length != PublicKeyBytes) { throw new KeyOutOfRangeException("publicKey", (publicKey == null) ? 0 : publicKey.Length, string.Format("key must be {0} bytes in length.", PublicKeyBytes)); } var final = ArrayHelper.ConcatArrays(new[] { CurveLockVersionPrefix }, publicKey); return(Base58CheckEncoding.Encode(final)); }
public void Encode_And_Decode_Hash() { var hash = Hash.Generate(); var data = hash.DumpByteArray(); var enCode = Base58CheckEncoding.Encode(data); enCode.ShouldNotBe(string.Empty); var deCode = Base58CheckEncoding.Decode(enCode); deCode.ShouldBe(data); }
public void Encode_And_Decode_Hash() { var hash = HashHelper.ComputeFrom("hash"); var data = hash.ToByteArray(); var enCode = Base58CheckEncoding.Encode(data); enCode.ShouldNotBe(string.Empty); var deCode = Base58CheckEncoding.Decode(enCode); deCode.ShouldBe(data); }
/// <summary> /// Base58Check encoding. /// </summary> /// <param name="payload">The message to encode.</param> /// <param name="prefix">The optional prefix to prepend.</param> /// <returns>Base58 encoded prefix + payload.</returns> public static string Encode(byte[] payload, byte[] prefix = null) { int prefixLen = prefix?.Length ?? 0; byte[] msg = new byte[prefixLen + payload.Length]; if (prefix != null) { Array.Copy(prefix, 0, msg, 0, prefix.Length); } Array.Copy(payload, 0, msg, prefixLen, payload.Length); return(Base58CheckEncoding.Encode(msg)); }
public void Encode_And_Decode_Address() { var address = Address.Generate(); var data = address.DumpByteArray(); var enCode = Base58CheckEncoding.Encode(data); enCode.ShouldNotBe(string.Empty); var deCode = Base58CheckEncoding.Decode(enCode); deCode.ShouldBe(data); var deCode1 = Base58CheckEncoding.DecodePlain(enCode); deCode1.ShouldNotBe(data); }
/// <summary> /// Converts address into base58 representation. /// </summary> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public string ToBase58() { if (_formattedAddress != null) { return(_formattedAddress); } if (Value.Length != AElfConstants.AddressHashLength) { throw new ArgumentException("Invalid address", nameof(Value)); } var pubKeyHash = Base58CheckEncoding.Encode(Value.ToByteArray()); return(_formattedAddress = pubKeyHash); }
public void TestRequireFooter() { string header = "test-header"; string footer = ""; byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer; try{ SimplePack packer = new SimplePack(header, footer); } catch (InvalidSimplePackHeader) { return; } Assert.Fail(); }
public void Encode_And_Decode_Address() { var address = Address.FromBase58("xFqJD9R33mQBQPr1hCFUZMayXFQ577j34MPyUdXzbPpAYufG2"); var data = address.ToByteArray(); var enCode = Base58CheckEncoding.Encode(data); enCode.ShouldNotBe(string.Empty); var deCode = Base58CheckEncoding.Decode(enCode); deCode.ShouldBe(data); var deCode1 = Base58CheckEncoding.DecodePlain(enCode); deCode1.ShouldNotBe(data); }
string GenerateAddress(PubKey pubKey) { var pk = pubKey.ToBytes(); HMACBlake2B blake2b = new HMACBlake2B(PKHASH_BIT_SIZE); blake2b.Initialize(); var pkNew = blake2b.ComputeHash(pk); int prefixLen = tz2.Length; byte[] msg = new byte[prefixLen + pkNew.Length]; Array.Copy(tz2, 0, msg, 0, tz2.Length); Array.Copy(pkNew, 0, msg, prefixLen, pkNew.Length); return(Base58CheckEncoding.Encode(msg)); }
public void Encode_Success() { string data = Base58CheckEncoding.Encode(ByteString.Parse("6f" + "d5188210339b2012cd8f3c5ce3773d49dd7baa4b").Value.ToArray()); Assert.Equal("mzwhamFUz1oFz2noTGDK9dxq3PAEhNkpuL", data); data = Base58CheckEncoding.Encode(ByteString.Parse("6f" + "4641508da141383ce2d1e035c58fad31480bcaac").Value.ToArray()); Assert.Equal("mmvRqdAcJrSv6M8GozQE4tR3DhfF56c5M1", data); data = Base58CheckEncoding.Encode(ByteString.Parse("6f" + "eeab6b6757f135e9ec2f47157c412f80493f1eca").Value.ToArray()); Assert.Equal("n3GvU9Zo74UZLYgHQcYiLgTFcWfErXFdwe", data); data = Base58CheckEncoding.Encode(ByteString.Parse("00" + "691290451961ad74e177bf44f32d9e2fe7454ee6").Value.ToArray()); Assert.Equal("1AaaBxiLVzo1xZSFpAw3Zm9YBYAYQgQuuU", data); data = Base58CheckEncoding.Encode(ByteString.Parse("05" + "36e0ea8e93eaa0285d641305f4c81e563aa570a2").Value.ToArray()); Assert.Equal("36hBrMeUfevFPZdY2iYSHVaP9jdLd9Np4R", data); }
public string ToPublicKeyString(string addressPrefix = null) { if (addressPrefix.IsNull()) { addressPrefix = ChainConfig.AddressPrefix; } var buffer = ToBuffer(true); var hash = RIPEMD160.Create().HashAndDispose(buffer); var checksum = hash.Slice(0, 4); hash.Clear(); var key = buffer.Concat(checksum); buffer.Clear(); checksum.Clear(); var result = addressPrefix + Base58CheckEncoding.Encode(key); key.Clear(); return(result); }
public string ToWif() { var priv = this.BN.ToByteArray(); byte[] res; if (this.Compressed) { res = new byte[34]; res[res.Length - 1] = 0x01; } else { res = new byte[33]; } Array.Copy(priv, 0, res, 1, priv.Length); res[0] = this.Network.PrivKeyPrefix; return(Base58CheckEncoding.Encode(res)); }
private async Task <Dictionary <string, string> > GetAndCreateAccountKey(DeployArg arg) { if (string.IsNullOrWhiteSpace(arg.ChainAccount)) { var keyStore = new AElfKeyStore(ApplicationHelper.AppDataPath); var chainPrefixBase58 = Base58CheckEncoding.Encode(ByteArrayHelpers.FromHexString(arg.SideChainId)); var chainPrefix = chainPrefixBase58.Substring(0, 4); var key = await keyStore.CreateAsync(arg.AccountPassword, chainPrefix); // todo clean //arg.ChainAccount = "ELF_" + chainPrefix + "_" + key.GetEncodedPublicKey(); } var fileName = arg.ChainAccount + ".ak"; var filePath = Path.Combine(ApplicationHelper.AppDataPath, "keys", fileName); var keyContent = File.ReadAllText(filePath); return(new Dictionary <string, string> { { fileName, keyContent } }); }
private void btnCrypt_Click(object sender, EventArgs e) { txtEncriptado.Text = ""; this.Refresh(); int blocklen = (n.GetBitsize() / 8) - 4; if (blocklen < 5) { MessageBox.Show("Chave Pequena", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } byte[] plaintextArr = Encoding.UTF8.GetBytes(txtMensagem.Text); UInt16 blockCnt = 0; String finalString = ""; foreach (byte[] plainBlock in plaintextArr.Slices(blocklen)) { byte[] withPadArr = new byte[plainBlock.Length + 4]; byte[] padArr = new byte[4]; rng.GetBytes(padArr); padArr[0] &= (Byte)0x7F; Array.Copy(padArr, withPadArr, 4); Array.Copy(plainBlock, 0, withPadArr, 4, plainBlock.Length); Array.Reverse(withPadArr); BigInteger toCrypt = new BigInteger(withPadArr); BigInteger crypt = BigInteger.ModPow(toCrypt, publicExp, n); byte[] cryptArr = crypt.ToByteArray(); String b58Enc = Base58CheckEncoding.Encode(cryptArr).ToString(); finalString += b58Enc + "_"; blockCnt++; } txtEncriptado.Text = finalString.TrimEnd('_'); }
public string encode(byte[] data) { // OutOfMemoryException may be thrown for large payloads return(head + Base58CheckEncoding.Encode(data) + foot); }
public void EncodeBitcoinAddress() { var actualText = Base58CheckEncoding.Encode(AddressBytes); Assert.AreEqual(ADDRESS_TEXT, actualText); }
public override string ToString() { return(Base58CheckEncoding.Encode(this.ToByteArray())); }
static void Main(string[] args) { // Bitcoin var privateKeyBitcoin = "L4Y1cGSsNv1Nf9dZpTkEyQjLU24zRyRQeRyE5i4MoVvrjrr15Koy"; // Example https://komodoplatform.com/en/academy/bitcoin-private-key/ var privateKeyBillText = new BitcoinSecret(privateKeyBitcoin, Network.Main); var uncompressed = privateKeyBillText.Copy(false); var privateKeyBill = uncompressed.ToBytes(); var publicKeyBillText = privateKeyBillText.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString(); var publicKeyBillTextUncompressed = uncompressed.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString(); // TODO Remove var publicKeyBillHex = Convert.ToHexString(uncompressed.PubKey.ToBytes().Skip(1).ToArray()); // Data var data = new byte[] { 1, 2, 3 }; // var hash = SHA256.Create().ComputeHash(data); // Calculate hash of data. See also ECDsa.SignHash(); // PrivateKey var privateKey = privateKeyBill; // new byte[32] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // RNGCryptoServiceProvider.Create().GetBytes(privateKey); // Fill random // PublicKey calculate var ecdsaCurve = ECCurve.CreateFromFriendlyName("secp256k1"); var ecdsa = ECDsa.Create(new ECParameters { Curve = ecdsaCurve, D = privateKey }); var publicAndPrivateKey = ecdsa.ExportParameters(true); // Sign var ecdsaSign = ECDsa.Create(new ECParameters { Curve = ecdsaCurve, D = publicAndPrivateKey.D, Q = publicAndPrivateKey.Q }); var signature = ecdsaSign.SignData(data, HashAlgorithmName.SHA256); // Needs private and public key // Verify var ecdsaVerify = ECDsa.Create(new ECParameters { Curve = ecdsaCurve, Q = publicAndPrivateKey.Q }); bool isValid = ecdsaVerify.VerifyData(data, signature, HashAlgorithmName.SHA256); // No private key needed! // Output var publicKey = Enumerable.Concat(publicAndPrivateKey.Q.X, publicAndPrivateKey.Q.Y).ToArray(); Console.WriteLine("PrivateKey=" + Convert.ToHexString(publicAndPrivateKey.D)); Console.WriteLine("PublicKey=" + Convert.ToHexString(publicKey)); Console.WriteLine("Data=" + Convert.ToHexString(data)); Console.WriteLine("IsValid=" + isValid); Debug.Assert(publicKeyBillHex == Convert.ToHexString(publicKey)); // P2PKH Address var x = new byte[65]; x[0] = 0x04; publicAndPrivateKey.Q.X.CopyTo(x, 1); publicAndPrivateKey.Q.Y.CopyTo(x, 33); var sha256 = System.Security.Cryptography.SHA256.Create().ComputeHash(x); var ripemd160 = RIPEMD160Managed.Create().ComputeHash(sha256); Debug.Assert(ripemd160.Length == 20); var publicKeyText = Base58CheckEncoding.Encode(Enumerable.Concat(new byte[] { 0 }, ripemd160).ToArray()); // Debug.Assert(publicKeyBillText == publicKeyText); // TODO Debug.Assert(publicKeyBillTextUncompressed == publicKeyText); // TODO Remove. Use compressed Console.WriteLine("Hello World!"); }