본문 바로가기
Study/Java

자바 암호화 sha- 256, sha-512

by 오늘만 사는 여자 2020. 11. 10.
728x90
반응형

암호화

 


sha-256

Version1

public static String EncBySha256(String data) throws Exception {
	  String retVal = "";
	  try {
	  	MessageDigest md = MessageDigest.getInstance("SHA-256");
	  	md.update(data.getBytes());

	  	byte byteData[] = md.digest();
	  	StringBuffer sb = new StringBuffer();
	    
	    for(int i=0; i<byteData.length; i++) {
	      sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
	    }

	    StringBuffer hexString = new StringBuffer();
	    for(int i=0; i<byteData.length;i++) {
	    String hex = Integer.toHexString(0xff & byteData[i]);
	    if(hex.length() == 1) {
	      hexString.append('0');
	    }
	      hexString.append(hex);
	    }

	  	retVal = hexString.toString();
	  } catch(NoSuchAlgorithmException e){
	  	System.out.println("EncBySHA256 Error:" + e.toString());
	  }
	  return retVal;
	}

 


Version2

public static String sha256(String msg) throws Exception {
	    MessageDigest md = MessageDigest.getInstance("SHA-256");
	    md.update(msg.getBytes());
	    StringBuilder builder = new StringBuilder();
	    for (byte b: md.digest()) {
		      builder.append(String.format("%02x", b));
		}
	    return builder.toString();
	}

Version3

public static String pwdEncrypt(String pwd) {
	    StringBuffer hexString = new StringBuffer();
	    try {
	        MessageDigest digest = MessageDigest.getInstance("SHA-256");
	        byte[] hash = digest.digest(pwd.getBytes("UTF-8"));
	        for (int i = 0; i < hash.length; i++) {
	            String hex = Integer.toHexString(0xff & hash[i]);
	            if (hex.length() == 1) {
	                hexString.append('0');
	            }
	            hexString.append(hex);
	        }
	    } catch (Exception ex) {
	        throw new RuntimeException(ex);
	    }
	 
	    return hexString.toString();
	}

Version4

public static String getSHA256(String input){

		String toReturn = null;
		try {
		    MessageDigest digest = MessageDigest.getInstance("SHA-256");
		    digest.reset();
		    digest.update(input.getBytes("utf8"));
		    toReturn = String.format("%064x", new BigInteger(1, digest.digest()));
		} catch (Exception e) {
		    e.printStackTrace();
		}
		
		return toReturn;
    }

Version5 sha-256 with salt

public static String getEncrypt(String source, String salt) {
        return getEncrypt(source, salt.getBytes());
    }
    
    public static String getEncrypt(String source, byte[] salt) {
        
        String result = "";
        
        byte[] a = source.getBytes();
        byte[] bytes = new byte[a.length + salt.length];
        
        System.arraycopy(a, 0, bytes, 0, a.length);
        System.arraycopy(salt, 0, bytes, a.length, salt.length);
        
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(bytes);
            
            byte[] byteData = md.digest();
            
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xFF) + 256, 16).substring(1));
            }
            
            result = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public static String generateSalt() {
        Random random = new Random();
        
        byte[] salt = new byte[8];
        random.nextBytes(salt);
        
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < salt.length; i++) {
            // byte 값을 Hex 값으로 바꾸기.
            sb.append(String.format("%02x",salt[i]));
        }
        
        return sb.toString();
    }

 


sha-512

Version1

	 public static String getSHA512(String input){

		String toReturn = null;
		try {
		    MessageDigest digest = MessageDigest.getInstance("SHA-512");
		    digest.reset();
		    digest.update(input.getBytes("utf8"));
		    toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
		} catch (Exception e) {
		    e.printStackTrace();
		}
		
		return toReturn;
     }

Version2

public static String encrypt(String input) throws NoSuchAlgorithmException {

		String output = "";
		StringBuffer sb = new StringBuffer();
		MessageDigest md = MessageDigest.getInstance("SHA-512");
		md.update(input.getBytes());
		byte[] msgb = md.digest();

		for (int i = 0; i < msgb.length; i++) {
			byte temp = msgb[i];
			String str = Integer.toHexString(temp & 0xFF);
			while (str.length() < 2) {
				str = "0" + str;
			}
			str = str.substring(str.length() - 2);
			sb.append(str);

		}
		output = sb.toString();
		return output;
	}

 

728x90
반응형

'Study > Java' 카테고리의 다른 글

RSA 암호화, 복호화  (0) 2020.11.12
[Java] AES256 암호화 및 복호화  (0) 2020.11.10
[FCM]파이어베이스에서 안드로이드로 보내기  (0) 2020.04.29
QR 코드, 바코드 생성하기  (1) 2020.02.27
Google Analtyics  (0) 2020.02.20

댓글