Created
April 30, 2010 02:40
-
-
Save x5gtrn/384642 to your computer and use it in GitHub Desktop.
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
public static void main(String[] args) throws Exception { | |
String inFilePath = args[0]; | |
int targetColumn = Integer.parseInt((args[1])); | |
String outFilePath = args[2]; | |
File inCsv = new File(inFilePath); | |
File outCsv = new File(outFilePath); | |
BufferedReader br = null; | |
FileInputStream fis = null; | |
PrintWriter pw = null; | |
FileOutputStream fos = null; | |
try { | |
fis = new FileInputStream(inCsv); | |
br = new BufferedReader(new InputStreamReader(fis, "UTF-8")); | |
fos = new FileOutputStream(outCsv); | |
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"))); | |
String line = null; | |
while (br.ready()) { | |
line = br.readLine(); | |
StringBuilder sb = new StringBuilder(); | |
StringTokenizer st = new StringTokenizer(line, ","); | |
for (int i = 1; st.hasMoreTokens(); i++) { | |
if (i > 1) { | |
sb.append(","); | |
} | |
if (i == targetColumn) { | |
sb.append(encrypt(st.nextToken())); | |
} else { | |
sb.append(st.nextToken()); | |
} | |
} | |
pw.println(sb.toString()); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (pw != null) { | |
pw.close(); | |
} | |
if (fos != null) { | |
fos.close(); | |
} | |
if (br != null) { | |
br.close(); | |
} | |
if (fis != null) { | |
fis.close(); | |
} | |
} catch (Exception ignore) { | |
} | |
} | |
} | |
private static String encrypt(String val) throws Exception { | |
val = val.substring(1, val.length() -1); | |
val = printDigest(getStringDigest(val)); | |
return "\"" + val + "\"" ; | |
} | |
private static byte[] getStringDigest(String data) throws Exception { | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
byte[] dat = data.getBytes(); | |
md.update(dat); | |
return md.digest(); | |
} | |
private static String printDigest(byte[] digest) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < digest.length; i++) { | |
int d = digest[i]; | |
if (d < 0) { | |
d += 256; | |
} | |
if (d < 16) { | |
sb.append("0"); | |
} | |
sb.append(Integer.toString(d, 16)); | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment