HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Hash-based cipher

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
basedhashcipher

Problem

I've written a cipher class based off of cryptographically secure hash functions and block cipher counter mode of operation. It currently runs around 20MB/s on my machine with the Java implementation of SHA256. The class is here as well as below.

Any critique or input on the design or code?

```
package blackdoor.crypto;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import blackdoor.struct.ByteQueue;
import blackdoor.util.Misc;

/**
* @author nfischer3
* Secure hash encryption, uses hash algorithms in CTR mode for mirrored, symmetric encryption.
*/
public class SHECipher implements Cipher{
public static final int MIN_KEY_SIZE = 8;

/**
*
* @return the default instance with a 256 bit block size
*/
public static SHECipher getDefaultInstance(){
try {
return new SHECipher(MessageDigest.getInstance("SHA-256"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}

private int blockNo = 0;
private boolean cfg = false;
private byte[] key;
private ByteQueue buffer;
private MessageDigest mD;
private IvParameterSpec iv;
private byte[] prehash;

public SHECipher(MessageDigest mD){
this.mD = mD;
}

public boolean isConfigured() {
return cfg;
}

public String getAlgorithm(){
return mD.getAlgorithm();
}

public byte[] getIV(){
return iv.getIV();
}

/**
* Initializes the cipher with key, creates a random IV to use with the cipher.
* @param key A key to encrypt with. Key can be any length over MIN_KEY_SIZE but a key longer than the block size will run more slowly.
* @return An IV that has been created for this cipher to u

Solution

I'm going to comment only on the coding aspects as I am not a cryptographer. The only thing I can say is that if you aren't one you shouldn't roll out your own algorithms.

/**
 * 
 */


What are these comments for? They don't convey any information, you should better delete them.

public static SHECipher getDefaultInstance(){


I'd suggest you to get rid of this factory method and replace it with dependency injection. It might look innocuous, but with that method you're just introducing an hidden dependency in your system, which is going to cause you troubles if you ever want to swap it with another implementation or if you want to test your code, which you should.

public void init(Key key, IvParameterSpec iv)


I'd get rid of the init method, and do all the initialisation in the constructor. Probably you want to decompose your class in two classes. A SHECiper, which represent cipher that is initialised and always in a valid state, and a SHECipherFactory that creates the instances you need and takes care of their initialisation. This would also allow you to make some of the fields of your class final, which is always a good thing.

public void reset()


Similary, you should get rid of reset too. If you don't need the cipher anymore just throw it away and create a new one instead.

Why do you keep code that is commented out? I'd delete all the zeroKey code. You're not using it anyway.

Code Snippets

/**
 * 
 */
public static SHECipher getDefaultInstance(){
public void init(Key key, IvParameterSpec iv)
public void reset()

Context

StackExchange Code Review Q#80197, answer score: 2

Revisions (0)

No revisions yet.