Guest User

Untitled

a guest
Jan 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import com.fasterxml.jackson.annotation.JsonIgnore;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4.  
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8.  
  9. /**
  10. * Compile the class with jackson-data bind dependency
  11. * <p>com.fasterxml.jackson.core<p/>
  12. */
  13. class TRBlock {
  14.  
  15. private static final String SHA1 = "SHA-1";
  16. private final static char[] hexArray = "0123456789abcdef".toCharArray();
  17.  
  18. public int index; // The index of the block
  19. public long timestamp; // Time stamp in epoch
  20. public Object data; // The data we want to store on the Block chain
  21. public String prevHash; // This is the chain/hash previous block
  22. public String nonce; // A Magical number that needs to be mined/found
  23. public int target; // Number of leading zeros in the current hash
  24. @JsonIgnore
  25. public String currHash; // Current hash
  26.  
  27. /**
  28. * Constructor for creating the block
  29. *
  30. * @param index the index of the block
  31. * @param timestamp the time stamp in epoch
  32. * @param data data to be stored in the block chain
  33. * @param prevHash hash of the previous block
  34. * @param nonce the nonce to be mined
  35. * @param target the number of leading zeros to mine the nonce
  36. */
  37. public TRBlock(int index, long timestamp, Object data, String prevHash,
  38. String nonce, int target) {
  39. this.index = index;
  40. this.timestamp = timestamp;
  41. this.data = data;
  42. this.prevHash = prevHash;
  43. this.nonce = nonce;
  44. this.target = target;
  45. }
  46.  
  47. /**
  48. * Convert a byte array into hexadecimal string
  49. * @param bytes data in bytes array
  50. * @return a string in hexadecimal
  51. */
  52. public static String bytesToHex(byte[] bytes) {
  53. char[] hexChars = new char[bytes.length * 2];
  54. for ( int j = 0; j < bytes.length; j++ ) {
  55. int v = bytes[j] & 0xFF;
  56. hexChars[j * 2] = hexArray[v >>> 4];
  57. hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  58. }
  59. return new String(hexChars);
  60. }
  61. /**
  62. * Generate SHA1 hash from a text
  63. * @param data the text data to be hashed
  64. * @return a string in base64 encoded format
  65. * @throws java.security.NoSuchAlgorithmException
  66. */
  67. public static String generateSHA1Hash(String data) throws NoSuchAlgorithmException {
  68. MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
  69. messageDigest.update(data.getBytes());
  70. byte [] hash = messageDigest.digest();
  71. messageDigest.reset();
  72. return bytesToHex(hash);
  73. }
  74.  
  75. /**
  76. * Convert TRBlock into json string
  77. * @param obj object to be stringyfied
  78. * @return json string of the object
  79. * @throws JsonProcessingException
  80. */
  81. public static String stringify(Object obj) throws JsonProcessingException {
  82. ObjectMapper objectMapper = new ObjectMapper();
  83. return objectMapper.writeValueAsString(obj);
  84. }
  85.  
  86. public static void main(String[] args) throws JsonProcessingException,
  87. UnsupportedEncodingException, NoSuchAlgorithmException {
  88. TRBlock block = new TRBlock(0, 1514528022, "Hello World",
  89. "000000000000000000000000000", "Random nonce", 8);
  90. System.out.println(generateSHA1Hash(stringify(block)));
  91. }
  92.  
  93. }
Add Comment
Please, Sign In to add comment