11 Replies - 190 Views - Last Post: 29 January 2018 - 12:12 PM Rate Topic: -----

#1 asd2791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-June 12

steps “block hash configuration”

Posted 29 January 2018 - 07:05 AM

The first step: Collect header data for blocks in one variable:
String ver="536870912";
 String prev_block="00000000000000000031093b660ac6e373c5f646f1d8809dba4e0ee9fe860ac7"; 
 String mrkl_root="39da9a8cd095d2f25c95098b43c79f81fd79cd4f5da1f6d670677683dce9bbe8";    
 String time="1517040023";                                                                 
 String bits="392962374";
 String nonce= "530744917";
 String Collect=ver+prev_block+mrkl_root+time+bits+nonce; 


The second step: generate sha256:

byte[] genSHA256 = genSHA256(Collect.getBytes());

The third step: Convert sha256 bytes To Hex:

String bytesToHex = bytesToHex(genSHA256);

Unfortunately, the result is wrong

7a11df9f78f74ad0adffae3e7ff874a7616c74bbe9f373b874265af0c35df70a


How can this code be corrected?

Is This A Good Question/Topic? 0
  • +

Replies To: steps “block hash configuration”

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: steps “block hash configuration”

Posted 29 January 2018 - 07:41 AM

Quote

Unfortunately, the result is wrong

Can you post the correct result to show what happened?

Have these methods been tested and shown to compute correctly:
genSHA256
bytesToHex
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,836
  • Joined: 20-September 08

Re: steps “block hash configuration”

Posted 29 January 2018 - 08:22 AM

Quote

byte[] genSHA256 = genSHA256(Collect.getBytes());


That's theoretically dangerous. That can return a different value depending on file.encoding. OK, it's not hugely likely since the content is mainly hex-numeric, but the point is it's not impossible. You should do

byte[] genSHA256 = genSHA256(Collect.getBytes("UTF-8"));


just in case
Was This Post Helpful? 0
  • +
  • -

#4 asd2791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-June 12

Re: steps “block hash configuration”

Posted 29 January 2018 - 08:53 AM

View PostNormR, on 29 January 2018 - 07:41 AM, said:

Quote

Unfortunately, the result is wrong

Can you post the correct result to show what happened?

Have these methods been tested and shown to compute correctly:
genSHA256
bytesToHex


I'm working on block number: 506326

The result should be:
00000000000000000043eefcebbd30810ad1a62b1abbed636ff7ec6521e741de

#506326
Was This Post Helpful? 0
  • +
  • -

#5 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: steps “block hash configuration”

Posted 29 January 2018 - 08:55 AM

Have these methods been tested and shown to compute correctly:
genSHA256
bytesToHex
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,836
  • Joined: 20-September 08

Re: steps “block hash configuration”

Posted 29 January 2018 - 09:03 AM

That time in your code is nothing like theirs
Was This Post Helpful? 0
  • +
  • -

#7 asd2791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-June 12

Re: steps “block hash configuration”

Posted 29 January 2018 - 09:17 AM

View PostNormR, on 29 January 2018 - 08:55 AM, said:

Have these methods been tested and shown to compute correctly:
genSHA256
bytesToHex


This is full code:
package block;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class DoubleSHA256 {

    final protected static char[] hexArray = "0123456789abcdef".toCharArray();

    public static byte[] genSHA256(byte[] input) {

        MessageDigest digester = null;
        try {
            digester = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return digester.digest(digester.digest(input));
    }

    private static String bytesToHex(final byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];

        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }

        return new String(hexChars);
    }

    public static void main(String[] args) {
     String ver="536870912";
     String prev_block="00000000000000000031093b660ac6e373c5f646f1d8809dba4e0ee9fe860ac7"; 
     String mrkl_root="39da9a8cd095d2f25c95098b43c79f81fd79cd4f5da1f6d670677683dce9bbe8";    
     String time="1517040023";                                                                 
     String bits="392962374";
     String nonce= "530744917"; 
     String Collect=ver+prev_block+mrkl_root+time+bits+nonce;
     
 
        byte[] genSHA256 = genSHA256(Collect.getBytes());
        
        String bytesToHex = bytesToHex(genSHA256);
        System.out.println(bytesToHex);

   
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,836
  • Joined: 20-September 08

Re: steps “block hash configuration”

Posted 29 January 2018 - 09:41 AM

So, what about the time value?
Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: steps “block hash configuration”

Posted 29 January 2018 - 09:44 AM

How have you tested the methods to see if they are working correctly?

This post has been edited by NormR: 29 January 2018 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 asd2791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-June 12

Re: steps “block hash configuration”

Posted 29 January 2018 - 10:41 AM

View Postg00se, on 29 January 2018 - 09:41 AM, said:

So, what about the time value?

Time is correct
See the original data of the block Click here
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,836
  • Joined: 20-September 08

Re: steps “block hash configuration”

Posted 29 January 2018 - 12:05 PM

I thought it was THIS ?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,836
  • Joined: 20-September 08

Re: steps “block hash configuration”

Posted 29 January 2018 - 12:12 PM

Sorry - they are the same. One is in Unix epoch seconds

What i'd like to know is where you get the evidence that the correct hash is derived from the concatenation you're doing?

This post has been edited by g00se: 29 January 2018 - 12:45 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1