Hashing Data
The TEAgent class of the Pi Soft Java TEAgent package provides functions for hashing individual data. The TEAgentMatrix class of the Pi Soft Java TEAgent package provides functions for hashing the columns in the matrix. Notice that a connection to a an Pi Soft kS is not required for hashing.
Code Samples
See the Java environment settings for details on setting the project environment.
Java_Hash
The following example demonstrates the use of the Java_Hash.
/*****************************************************************************************
*
* Java_Hash.java
*
******************************************************************************************/
import com.eruces.teagent.TEAgentConnection;
import com.eruces.teagent.TEAgentException;
import com.eruces.teagent.TEAgentConnectionFactory;
import com.eruces.teagent.TEAgentMatrix;
import java.util.TreeMap;
public class Java_Hash
{
public void testHash() throws TEAgentException
{
// Hash a single data
String strSingleData = "test string";
String strSalt = "test salt";
String strHashed = "";
TEAgentMatrix matrix = new TEAgentMatrix();
strHashed = matrix.performHash(strSingleData, strSalt); // SHA-256
System.out.println("Data = " + strSingleData);
System.out.println("Salt = " + strSalt);
System.out.println("Hash value = " + strHashed);
// Hash a column in a matrix
int nRows = 2;
int nCols = 3;
int i, j;
String strData [][] = new String[nRows][nCols];
String strHashVals [] = new String[nRows];
String strSalt2 = "another test salt";
// Set data to be encrypted
for(i = 0; i < nRows; i++)
{
strHashVals[i] = ""; // HiddenLink set to null
for(j = 0; j < nCols; j++)
strData[i][j] = "test string" + "(" + i + ", " + j + ")"; // Data to be encrypted
}
// Set the matrix for hashing
matrix = new TEAgentMatrix();
for(i = 0; i < strData.length; i++)
{
// Set data for encryption. For TEAgentMatrix, both row count and column count starts at 1
for(j = 0; j < strData[i].length; j++)
matrix.setRawElement(strData[i][j].getBytes(), i+1, j+1);
}
// Decide which columns to hash, also set salt.
int nRow2Hash = 1;
TreeMap treeMap = new TreeMap();
treeMap.put(new Integer(nRow2Hash), strSalt2.getBytes()); // hash column 1
matrix.setHashColumns(treeMap);
// Get hashed value.
System.out.println();
System.out.println( "Hash value for column " + nRow2Hash + ":");
for(i = 1; i <= matrix.getRowCount(); i++)
{
String strTemp = new String(matrix.getRawElement(i, nRow2Hash));
strHashVals[i-1] = matrix.getHashElement(i, nRow2Hash, "", 1 ); // row number, col number, salt, algorithm
System.out.println( "Data: " + strTemp + " Hash value: " + strHashVals[i-1]);
}
}
public static void main(String[] args)
{
try
{
Java_Hash java_hash = new Java_Hash();
java_hash.testHash();
}
catch (TEAgentException te)
{
te.printStackTrace();
System.exit(0);
}
}
}
Data = test string
Salt = test salt
Hash value = vE8B4Tfr2w+XlLjdJedsig==
Hash value for column 1:
Data: test string(0, 0) Hash value: LkFJK2Zs6hdIww2KRm67yw==
Data: test string(1, 0) Hash value: 2LgKW8VXVoAT9POy/9gFnw==