How to check if the first n bytes of a string are zeroes?

248
May 21, 2017, at 03:10 AM
long nonce;
String message = "blahblabahlsdhqwi";
String digest = digest("SHA-256", String + nonce);
byte[] digestBytes = digest.getBytes();

I'm trying to hash through a message whilst incrementing a nonce until I find a digest that has the first 4 bytes that are 0's. How can I do this?

Answer 1

You could use an IntStream with a limit(n) (to take the first n numbers) and allMatch. Like,

int n = 4;
if (IntStream.range(0, digestBytes.length).limit(n)
        .allMatch(i -> digestBytes[i] == 0)) {
    // ...
}

or just

int n = 4;
if (IntStream.range(0, n).allMatch(i -> digestBytes[i] == 0)) {
    // ...
}
READ ALSO
how to change the background color of a J Checkbox inside the actual checkbox

how to change the background color of a J Checkbox inside the actual checkbox

How to change the color of the actual box of a JCheckBox?

299
Showing Tick/Cross big symbol on correct/incorrect answer

Showing Tick/Cross big symbol on correct/incorrect answer

I have an android layout with an image view for question figure and two buttons for possible answers, one of them is for correctI want that if I press correct button a big Tick green symbol should display (but background should be still visible) indicating...

196
Develop java we app on local and deploy Heroku

Develop java we app on local and deploy Heroku

I am new on Heroku cloud platformIs there any way we can develop java web based application on Eclipse Tomcat, test & finally deploy on Heroku cloud platform

245
java.lang.IllegalStateException: Can't get ClosableLazy value after it has been closed

java.lang.IllegalStateException: Can't get ClosableLazy value after it has been closed

I am working on a Rest API implemented using Play Framework 23

240