Hello.
I'm trying to create a checksum of a fixed entropy, the same as the one calculated in the tutorial:
https://learnmeabitcoin.com/technical/mnemonic. The tutorial teaches us in ruby, so I need to convert from ruby to java:
For this, I am using the website: https://algodaily.com/convert/ruby/java.
The problem is that I'm not getting the same result as the tutorial checksum.
Do you know what could be wrong?
my example is calculating 01, and the tutorial calculates 00.
ruby:
my project test attach
I'm trying to create a checksum of a fixed entropy, the same as the one calculated in the tutorial:
https://learnmeabitcoin.com/technical/mnemonic. The tutorial teaches us in ruby, so I need to convert from ruby to java:
For this, I am using the website: https://algodaily.com/convert/ruby/java.
The problem is that I'm not getting the same result as the tutorial checksum.
Do you know what could be wrong?
my example is calculating 01, and the tutorial calculates 00.
ruby:
Create checksum:
# ----------------------
# 2. Entropy to Mnemonic
# ----------------------
entropy = "1010110111011000110010010010111001001011001001010110001011100001"
# 1. Create checksum
require 'digest'
size = entropy.length / 32 # number of bits to take from hash of entropy (1 bit checksum for every 32 bits entropy)
sha256 = Digest::SHA256.digest([entropy].pack("B*")) # hash of entropy (in raw binary)
checksum = sha256.unpack("B*").join[0..size-1] # get desired number of bits
puts "checksum: #{checksum}"
java:
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.math.BigInteger;
public static String genchecksum(String mEntropy) throws Exception {
String entropy = mEntropy;
// 1. Create checksum
int size = entropy.length() / 32; // Number of bits to take from hash of entropy (1 bit checksum for every 32 bits entropy)
BA.Log("size: " + size);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] sha256 = digest.digest(new BigInteger(entropy, 2).toByteArray()); // Hash of entropy (in raw binary)
BA.Log("byte: " + sha256);
String binarySha256 = new BigInteger(1, sha256).toString(2);
// Ensure leading 0's are not lost
while (binarySha256.length() < sha256.length * 8) {
binarySha256 = "0" + binarySha256;
}
String checksum = binarySha256.substring(0, size); // Get desired number of bits
return checksum;
}
my project test attach