Created
July 7, 2017 19:33
-
-
Save matthewtckr/39e4d5b154f0f0c734b7e521439312ee to your computer and use it in GitHub Desktop.
Hash All Strings in Row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Map; | |
import java.util.HashMap; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
List stringFields; | |
MessageDigest digest; | |
Map digestFormats; | |
// Edit value to match desired algorithm | |
private final String algorithm = "SHA-256"; | |
public boolean init(StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface) { | |
if ( !parent.initImpl(stepMetaInterface, stepDataInterface) ) { | |
return false; | |
} | |
digestFormats = new HashMap(); | |
digestFormats.put( "MD5", "%032x" ); | |
digestFormats.put( "SHA-1", "%040x" ); | |
digestFormats.put( "SHA-256", "%064x" ); | |
stringFields = new ArrayList(); | |
try { | |
digest = MessageDigest.getInstance( algorithm ); | |
} catch ( NoSuchAlgorithmException nsae ) { | |
return false; | |
} | |
return true; | |
} | |
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { | |
Object[] r = getRow(); | |
if ( r == null ) { | |
setOutputDone(); | |
return false; | |
} | |
if ( first ){ | |
first = false; | |
RowMetaInterface rowMeta = getInputRowMeta(); | |
for ( int i = 0; i < rowMeta.size(); i++ ) { | |
if ( rowMeta.getValueMeta( i ).isString() ) { | |
stringFields.add( i ); | |
} | |
} | |
} | |
r = createOutputRow( r, data.outputRowMeta.size() ); | |
for ( int i = 0; i < stringFields.size(); i++ ) { | |
int fieldIndex = (Integer) stringFields.get( i ); | |
String value = (String) r[fieldIndex]; | |
if ( value != null ) { | |
digest.reset(); | |
digest.update( value.getBytes() ); | |
BigInteger tmpValue = new BigInteger( 1, digest.digest() ); | |
String newValue = String.format( (String) digestFormats.get( algorithm ), new Object[] { tmpValue } ); | |
logDebug( "New: " + newValue + " / Old: " + value ); | |
r[fieldIndex] = newValue; | |
} | |
} | |
// Send the row on to the next step. | |
putRow( data.outputRowMeta, r ); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment