1. import java.security.MessageDigest;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Date;
    4. private SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    5. public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    6. if (first) {
    7. first = false;
    8. }
    9. Object[] r = getRow();
    10. if (r == null) {
    11. setOutputDone();
    12. return false;
    13. }
    14. r = createOutputRow(r, data.outputRowMeta.size());
    15. String password = get(Fields.In, "password").getString(r);
    16. String yyMMdd = sdf.format(new Date());
    17. String encry_origin = password+yyMMdd;
    18. password = encode(encry_origin);
    19. get(Fields.Out, "password").setValue(r, password);
    20. putRow(data.outputRowMeta, r);
    21. return true;
    22. }
    23. public static String encode(String str) {
    24. if (str == null) {
    25. return null;
    26. }
    27. try {
    28. MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
    29. messageDigest.update(str.getBytes());
    30. return getFormattedText(messageDigest.digest());
    31. } catch (Exception e) {
    32. throw new RuntimeException(e);
    33. }
    34. }
    35. private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
    36. '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    37. private static String getFormattedText(byte[] bytes) {
    38. int len = bytes.length;
    39. StringBuilder buf = new StringBuilder(len * 2);
    40. // 把密文转换成十六进制的字符串形式
    41. for (int j = 0; j < len; j++) {
    42. buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
    43. buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
    44. }
    45. return buf.toString();
    46. }