Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

we are given a message, M, and the current time, T, and we must generate some set of bytes R such that SHA256(M + R

we are given a message, M, and the current time, T, and we must generate some set of bytes R such that SHA256(M + R + T) have a certain number of starting bits, P, set. for example, if P=1, you could set R to nothing and just hash SHA256(M+T) together and you have a 50% chance of getting the first bit set. if the resulting first bit is set, you will change R to something else and try again. the idea is to keep changing R until you get the right number of unset bits at the beginning of the hash result.

for this assignment you will need to implement the single method doWork() in the ProofOfWork.javaimage text in transcribed class. it returns the R that you found that produces the correct hash result.

package proj4; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import java.math.BigInteger; import java.security.MessageDigest; @FixMethodOrder(MethodSorters.DEFAULT) public class ProofOfWorkTest { static byte bigMessage[]; static { byte smallMessage[] = "This is a small message".getBytes(); bigMessage = new byte[1024*1024]; for (int i = 0; i bigMessage.length; i++) { bigMessage[i] = smallMessage[i%smallMessage.length]; } printHash(bigMessage); /* do a warmup */ new ProofOfWorkTest().checkXBit(16); } static void printHash(byte result[]) { StringBuilder sb = new StringBuilder(); for (int i = 0; i > shift; sb.append((result[i] & m) != 0 ? '1' : '0'); } sb.append(' '); if (result[i] != 0) { break; } } System.out.println(sb); } static boolean allUnset(byte result[], int count) { for (int i = 0; i > (i % 8); if ((b & mask) > 0) { return false; } } return true; } public void checkXBit(int count) { try { byte[] time = BigInteger.valueOf( System.currentTimeMillis()).toByteArray(); byte result[] = ProofOfWork.doWork(bigMessage, time, count); MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(bigMessage); digest.update(result); byte hash[] = digest.digest(time); System.out.println(count); printHash(hash); Assert.assertTrue(allUnset(hash, count)); } catch (Exception e) { e.printStackTrace(); } } @Test public void check01Bit() { checkXBit(1); } @Test public void check04Bit() { checkXBit(4); } @Test public void check08Bit() { checkXBit(8); } @Test public void check16Bit() { checkXBit(16); } @Test public void check18Bit() { checkXBit(18); } @Test public void check20Bit() { checkXBit(20); } @Test public void check24Bit() { checkXBit(24); } @Test public void check25Bit() { checkXBit(25); } @Test public void check26Bit() { checkXBit(26); } @Test public void check27Bit() { checkXBit(27); } @Test public void check28Bit() { checkXBit(28); } } 

_______________________________________________________

package proj4; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class ProofOfWork { /**  * find an array of bytes such that the SHA256 hash of the message,  * bytes, and time bytes will produce a result where the first  * numberOfUnsetBits will be unset (0).  *  * @param message the message bytes.  * @param time the time bytes.  * @param numberOfUnsetBits the number of initial bits of the hash result  * that must be unset (equal to 0).  * @return the array of bytes that will produce numberOfUnsetBits initial  * zero bits from SHA256(message + bytes + time).  */  static byte[] doWork(byte message[], byte time[], int numberOfUnsetBits) throws NoSuchAlgorithmException { return null; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions