'hash'에 해당되는 글 4건

  1. 2018.09.28 [개발일반] Hashing 하기
  2. 2016.07.08 [Java] Hash Algorithm 테스트
  3. 2014.08.27 [ElasticSearch] Hash Partition 테스트
  4. 2013.08.27 [java] MD5 Hash 구현.

[개발일반] Hashing 하기

ITWeb/개발일반 2018. 9. 28. 16:55

$ echo -n 'Helloworld' | shasum -a 256

$ pwgen -N 1 -s 32

$ printf '%s' "Helloworld" | md5sum

$ printf '%s' "Helloworld" | md5


터미널 상에서 hashing 이 필요 할때 쓰면 되겠내요.

:

[Java] Hash Algorithm 테스트

ITWeb/개발일반 2016. 7. 8. 11:19

그냥 인터넷에 돌아 다니는 코드 가져가 기록해 봤습니다.


[코드]

public class HashGenerator {

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

String md2 = getHash("test", "md2");
String md5 = getHash("test", "md5");
String sha1 = getHash("test", "sha1");
String sha256 = getHash("test", "sha-256");
String sha384 = getHash("test", "sha-384");
String sha512 = getHash("test", "sha-512");

// “MD2″, “MD5″, “SHA1″, “SHA-256″, “SHA-384″, “SHA-512″
System.out.println("MD2 : [" + md2 + "](" + md2.length() + ")");
System.out.println("MD5 : [" + md5 + "](" + md5.length() + ")");
System.out.println("SHA1 : [" + sha1 + "](" + sha1.length() + ")");
System.out.println("SHA-256 : [" + sha256 + "](" + sha256.length() + ")");
System.out.println("SHA-384 : [" + sha384 + "](" + sha384.length() + ")");
System.out.println("SHA-512 : [" + sha512 + "](" + sha512.length() + ")");
}

public static String getHash(String message, String algorithm)
throws NoSuchAlgorithmException {

try {
byte[] buffer = message.getBytes();
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(buffer);
byte[] digest = md.digest();
String hex = "";

for(int i = 0 ; i < digest.length ; i++) {
int b = digest[i] & 0xff;
if (Integer.toHexString(b).length() == 1) hex = hex + "0";
hex = hex + Integer.toHexString(b);
}

return hex;
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}

return null;
}
}


[결과]

MD2     : [dd34716876364a02d0195e2fb9ae2d1b](32)

MD5     : [098f6bcd4621d373cade4e832627b4f6](32)

SHA1    : [a94a8fe5ccb19ba61c4c0873d391e987982fbbd3](40)

SHA-256 : [9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08](64)

SHA-384 : [768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9](96)

SHA-512 : [ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff](128)


:

[ElasticSearch] Hash Partition 테스트

Elastic/Elasticsearch 2014. 8. 27. 14:15

간혹 특정 shard 로 색인 문서가 몰리는 경우가 있습니다.

이럴경우 _id 값에 대한 key 조합을 확인해야 할 필요가 있는데요.

es 내부에서 사용하는 hash 함수를 이용해서 간단하게 테스트 해볼수 있습니다.


[테스트 코드]

public class EsHashPartitionTest {

    private static final Logger log = LoggerFactory.getLogger(EsHashPartitionTest.class);

    private HashFunction hashFunction = new DjbHashFunction();

    


    @Test

    public void testHashPartition() {

        int shardSize = 120;

        List<Long> shards = new ArrayList<Long>();

        long[] partSize = new long[shardSize];

        

        for ( int i=0; i<shardSize; i++ ) {

            shards.add((long) 0);

            partSize[i] = 0;

        }

        

        for ( int i=0; i<1000000; i++ ) {

            int shardId = MathUtils.mod(hash(String.valueOf(i)), shardSize);

            shards.add(shardId, (long) ++partSize[shardId]);

        }

        

        for ( int i=0; i<shardSize; i++ ) {

            log.debug("["+i+"] {}", partSize[i]);

        }

    }

    

    public int hash(String routing) {

        return hashFunction.hash(routing);

    }

}


[Hash 함수 원본 코드]

/**

 * This class implements the efficient hash function

 * developed by <i>Daniel J. Bernstein</i>.

 */

public class DjbHashFunction implements HashFunction {


    public static int DJB_HASH(String value) {

        long hash = 5381;


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

            hash = ((hash << 5) + hash) + value.charAt(i);

        }


        return (int) hash;

    }


    public static int DJB_HASH(byte[] value, int offset, int length) {

        long hash = 5381;


        final int end = offset + length;

        for (int i = offset; i < end; i++) {

            hash = ((hash << 5) + hash) + value[i];

        }


        return (int) hash;

    }


    @Override

    public int hash(String routing) {

        return DJB_HASH(routing);

    }


    @Override

    public int hash(String type, String id) {

        long hash = 5381;


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

            hash = ((hash << 5) + hash) + type.charAt(i);

        }


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

            hash = ((hash << 5) + hash) + id.charAt(i);

        }


        return (int) hash;

    }

}

- https://github.com/elasticsearch/elasticsearch/tree/master/src/main/java/org/elasticsearch/cluster/routing/operation/hash


이와 관련된 자세한 내용은 아래 링크 참고하세요.

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/routing-value.html

:

[java] MD5 Hash 구현.

ITWeb/개발일반 2013. 8. 27. 18:34

MessageDigest 랑 Spring 의 DigestUtils 로 구현된 예제 입니다.


public static String getMd5(String input) throws Exception {

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

//        md.update(Constants.HASH_KEY_PHRASE.getBytes());

// 

//        byte[] bytesInput = input.getBytes("UTF-8");

//        byte byteData[] = md.digest(bytesInput);        

//        StringBuffer strBuffer = new StringBuffer();

//        

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

//                strBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

//        }

//        

//        return strBuffer.toString();


           byte[] bytesInput = input.getBytes("UTF-8");

           String cipher = DigestUtils.md5DigestAsHex(bytesInput);


           return cipher;

}


: