Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/codeguru.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015 M. Isuru Tharanga Chrishantha Perera
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.chrishantha.sample.highcpu;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class HashingWorker implements Runnable {

private final String algorithm;
private final long length;

private byte[] lastComputedHash;

public HashingWorker(long length, String algorithm) {
this.length = length;
this.algorithm = algorithm;
}

@SuppressWarnings("InfiniteLoopStatement")
@Override
public void run() {
while (true) {
StringBuilder data = new StringBuilder();

// Some random data
for (int i = 0; i < length; i++) {
data.append(UUID.randomUUID().toString());
}

MessageDigest digest;
try {
digest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}

// Hash
digest.update(data.toString().getBytes());
byte[] computedHash = digest.digest();
if (lastComputedHash != null && computedHash.length != lastComputedHash.length) {
throw new IllegalStateException("Is the hash computation correct??");
}
lastComputedHash = computedHash;
}
}
}