@Test(expected = RecipeException.class) public void testBadAlgorithm() throws Exception { String[] directives = new String[] { "hash message1 SHA-385 true", }; List<Row> rows = Arrays.asList( new Row("message1", "This is a very secret message and a digest will be created.") ); TestingRig.execute(directives, rows); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String' or 'byte array'.", column)); } byte[] message; if (object instanceof String) { message = ((String) object).getBytes(StandardCharsets.UTF_8); } else if (object instanceof byte[]) { message = ((byte[]) object); } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String' or 'byte array'.", column, object.getClass().getSimpleName())); } digest.update(message); byte[] hashed = digest.digest(); if (encode) { String hasedHex = String.format("%064x", new java.math.BigInteger(1, hashed)); row.addOrSet(column, hasedHex); } else { row.addOrSet(column, hashed); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }
MessageHash implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String' or 'byte array'.", column)); } byte[] message; if (object instanceof String) { message = ((String) object).getBytes(StandardCharsets.UTF_8); } else if (object instanceof byte[]) { message = ((byte[]) object); } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String' or 'byte array'.", column, object.getClass().getSimpleName())); } digest.update(message); byte[] hashed = digest.digest(); if (encode) { String hasedHex = String.format("%064x", new java.math.BigInteger(1, hashed)); row.addOrSet(column, hasedHex); } else { row.addOrSet(column, hashed); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; } }
MessageHash implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String' or 'byte array'.", column)); } byte[] message; if (object instanceof String) { message = ((String) object).getBytes(StandardCharsets.UTF_8); } else if (object instanceof byte[]) { message = ((byte[]) object); } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String' or 'byte array'.", column, object.getClass().getSimpleName())); } digest.update(message); byte[] hashed = digest.digest(); if (encode) { String hasedHex = String.format("%064x", new java.math.BigInteger(1, hashed)); row.addOrSet(column, hasedHex); } else { row.addOrSet(column, hashed); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  }
MessageHash implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String' or 'byte array'.", column)); } byte[] message; if (object instanceof String) { message = ((String) object).getBytes(StandardCharsets.UTF_8); } else if (object instanceof byte[]) { message = ((byte[]) object); } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String' or 'byte array'.", column, object.getClass().getSimpleName())); } digest.update(message); byte[] hashed = digest.digest(); if (encode) { String hasedHex = String.format("%064x", new java.math.BigInteger(1, hashed)); row.addOrSet(column, hasedHex); } else { row.addOrSet(column, hashed); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override Mutation lineage(); static boolean isValid(String algorithm); @Override List<Row> execute(List<Row> rows, ExecutorContext context); }
MessageHash implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String' or 'byte array'.", column)); } byte[] message; if (object instanceof String) { message = ((String) object).getBytes(StandardCharsets.UTF_8); } else if (object instanceof byte[]) { message = ((byte[]) object); } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String' or 'byte array'.", column, object.getClass().getSimpleName())); } digest.update(message); byte[] hashed = digest.digest(); if (encode) { String hasedHex = String.format("%064x", new java.math.BigInteger(1, hashed)); row.addOrSet(column, hasedHex); } else { row.addOrSet(column, hashed); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override Mutation lineage(); static boolean isValid(String algorithm); @Override List<Row> execute(List<Row> rows, ExecutorContext context); static final String NAME; }