2014. 9. 25. 18:10 카테고리 없음

HASH값 바꾸기 


어쩌다 보니 HASH값 바꿀 상황이 생겨서 

이미지는 바이트 하나 더 늘려서 거기다가 랜덤값 넣고

XML은 주석 추가

JAVA 소스도 주석 추가 

하는 방법으로 해시코드값 변경했다 


JAVA, XML주석 추가로 해쉬코드 변경은 컴파일 하면 의미없다 ..




다른 좋은 방법이있다면 알려주세요 ~~~~ 



import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.security.MessageDigest;

import java.util.Random;


public class HashCode {

public int byteplusvalue = 8;

public void print(File f, String type) throws Exception {

MessageDigest md = MessageDigest.getInstance(type);

// MessageDigest md = MessageDigest.getInstance("MD5");

FileInputStream fis = new FileInputStream(f);


byte[] dataBytes = new byte[1024 * 10];


int nread = 0;

while ((nread = fis.read(dataBytes)) != -1) {

md.update(dataBytes, 0, nread);

};

byte[] mdbytes = md.digest();


//convert the byte to hex format method 1

StringBuffer sb = new StringBuffer();

for (int i = 0; i < mdbytes.length; i++) {

sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));

}


System.out.println(f.getName() + "\t" + type + "\t" + "Digest(in hex format):: " + sb.toString());


// //convert the byte to hex format method 2

// StringBuffer hexString = new StringBuffer();

// for (int i = 0; i < mdbytes.length; i++) {

// String hex = Integer.toHexString(0xff & mdbytes[i]);

// if (hex.length() == 1)

// hexString.append('0');

// hexString.append(hex);

// }

// System.out.println(f.getName() + "Digest(in hex format):: " + hexString.toString());

}


String rootDir;


/**

* @param args

* 0 루트 경로

* 1 랜덤값

*/

public HashCode(String[] args) {

rootDir = args[0];

startSearch(new File(rootDir));

}


public void startSearch(File f) {


File[] listOfFiles = f.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) {

ChangeFile(listOfFiles[i]);

}

else if (listOfFiles[i].isDirectory()) {

startSearch(listOfFiles[i]);

}

}

}


public void ChangeFile(File f) {

String name = f.getName();

System.out.println(name);

int index = name.lastIndexOf(".");

if (index == 0 || index == -1) {

System.out.println(name + " error " + index);

return;

}

String fileType = name.substring(index + 1, name.length());


if (!(fileType.equals("xml") || fileType.equals("png") || fileType.equals("java"))) {

System.out.println(name + " " + fileType + " error");

return;

}


FileInputStream fin = null;

byte fileContent[] = null;

try {


print(f, "MD5");

print(f, "SHA-1");

fin = new FileInputStream(f);


if (fileType.equals("xml"))

fileContent = new byte[(int) f.length()];

else if (fileType.equals("png"))

fileContent = new byte[(int) f.length() + byteplusvalue];

else if (fileType.equals("java"))

fileContent = new byte[(int) f.length() ];

fin.read(fileContent);

}

catch (Exception e) {

e.printStackTrace();

}

finally {

try {

if (fin != null)

fin.close();

}

catch (Exception e) {


}

}


if (fileContent == null)

return;


DataOutputStream dos = null;

try {


dos = new DataOutputStream(new FileOutputStream(f));

if (fileType.equals("xml") ) {


Random r = new Random();

String str = new String(fileContent,"8859_1");


str += "\n <!--" + r.nextInt() + "-->";

dos.writeBytes(str);

}else if( fileType.equals("java"))

{

Random r = new Random();

String str = new String(fileContent,"8859_1");


str += "\n //<!--" + r.nextInt() + "-->";

dos.writeBytes(str);

}

else if (fileType.equals("png"))

{

Random r = new Random();

int plusvalue = r.nextInt();

fileContent[fileContent.length-1]=(byte)plusvalue;

dos.write(fileContent);

}


}

catch (Exception e) {

e.printStackTrace(System.err);

}

finally {

try {

if (dos != null)

dos.close();

}

catch (Exception e) {


}

}

try {

print(f, "MD5");

print(f, "SHA-1");

}

catch (Exception e) {


}


}


public static void main(String[] args) throws Exception {

HashCode hash = new HashCode(args);


}


}



posted by 욱이다