본문 바로가기

JAVA

SHA-512 인코딩

package info.rue.app.common.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CommonPasswordUtil {

private final byte[] PREFIX_SALT_PASSWORD = {
           0    , 22    , 67    , 3     , -88   , 27    , -89   , -17   
        , -2    , 21    , -100  , 45    , -24   , -100  , 88    , 39    
        , -25   , -49   , 7     , -3    , -26   , 100   , -108  , 11    
        , 83    , 13    , 43    , 126   , -32   , 37    , 122   , -33
};

private final byte[] SUFFIX_SALT_PASSWORD = {
          4     , 83    , -34   , -122  , 97    , -86   , -8    , 37    
        , -128  , 36    , 43    , 27    , 64    , 81    , 74    , -10   
        , 12    , -125  , 24    , -74   , 77    , -76   , 1     , -57   
        , -3    , -54   , 110   , 47    , 32    , 39    , 62    , 66    
};

public byte[] getRandomByte() {
    byte[] randomBytes = new byte[64];

    try {
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.nextBytes(randomBytes);
        log.debug("{}, {}", randomBytes.length, randomBytes);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return randomBytes;
}

public char[] convertByteToChar(byte[] b) {
    if(b == null) {
        return null;
    }

    char[] c = new char[b.length];

    for(int i = 0; i < b.length; i++) {
        c[i] = (char) (b[i] & 0xFF);
    }

    return c;
}

public String getEncode(String passwordType, String password) throws NoSuchAlgorithmException {
    return getEncode(passwordType, password, false);
}

public String getEncode(String passwordType, String password, boolean isSalt) throws NoSuchAlgorithmException {
    String p = null;

    if("SHA-512".equals(passwordType)) {
        p = getEncodeForSha512(password, isSalt);
    }

    return p;
}

private String getEncodeForSha512(String password, boolean isSalt) throws NoSuchAlgorithmException {
    String p = null;
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
        messageDigest.update(!isSalt ? "".getBytes():PREFIX_SALT_PASSWORD);
        messageDigest.update(password.getBytes());
        messageDigest.update(!isSalt ? "".getBytes():SUFFIX_SALT_PASSWORD);
        byte[] b = messageDigest.digest();
        p = String.format("%0128X", new BigInteger(1, b));
        log.debug("{}", p);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw e;
    }

    return p;
}

public static void main(String[] args) throws Exception {
    CommonPasswordUtil commonPasswordUtil = new CommonPasswordUtil();
    byte[] salt = commonPasswordUtil.getRandomByte();
    byte[] prefixSalt = new byte[salt.length/2];
    byte[] suffixSalt = new byte[salt.length/2];

    System.arraycopy(salt, 0, prefixSalt, 0, prefixSalt.length);
    System.arraycopy(salt, prefixSalt.length, suffixSalt, 0, suffixSalt.length);
    log.debug("{}", prefixSalt);
    log.debug("{}", suffixSalt);
    for(int i = 0; i < 64; i++) {
        System.out.print(", " + salt[i] + "\t");
        if(i%8 == 7) {
            System.out.println("");
        }
    }

    String p = null;
    try {

        MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
        messageDigest.update(prefixSalt);
        messageDigest.update("12345678".getBytes());
        messageDigest.update(suffixSalt);
        byte[] b = messageDigest.digest();
        p = String.format("%0128X", new BigInteger(1, b));

        log.debug("{}", p);

        messageDigest.reset();

        messageDigest = MessageDigest.getInstance("SHA-512");
        messageDigest.update(commonPasswordUtil.PREFIX_SALT_PASSWORD);
        messageDigest.update("12345678".getBytes());
        messageDigest.update(commonPasswordUtil.SUFFIX_SALT_PASSWORD);
        b = messageDigest.digest();
        p = String.format("%0128X", new BigInteger(1, b));

        log.debug("{}", p);

        messageDigest.reset();

        messageDigest.update("".getBytes());
        messageDigest.update("12345678".getBytes());
        messageDigest.update("".getBytes());
        b = messageDigest.digest();
        p = String.format("%0128X", new BigInteger(1, b));
        log.debug("{}", p);

        messageDigest.reset();

        messageDigest.update("12345678".getBytes());
        b = messageDigest.digest();
        p = String.format("%0128X", new BigInteger(1, b));
        log.debug("{}", p);

        commonPasswordUtil.getEncode("SHA-512", "12345678");
        commonPasswordUtil.getEncode("SHA-512", "12345678", false);
        commonPasswordUtil.getEncode("SHA-512", "12345678", true);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw e;
    }

}

}