Message Digest とは?

プログラム ( MDを使ったUID生成プログラム)

package com.snail.example;

import sun.misc.HexDumpEncoder;

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MessageDigestExam {
  
  private static HexDumpEncoder hex = new HexDumpEncoder();

  public static void main(String[] args) {
    try {
      byte[] uid1 = getUID("MD2");
      byte[] uid2 = getUID("MD5");
      byte[] uid3 = getUID("SHA-1");
      byte[] uid4 = getUID("SHA-256");
      byte[] uid5 = getUID("SHA-384");
      byte[] uid6 = getUID("SHA-512");
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }

  private static byte[] getUID(String algorithm)
    throws UnknownHostException, NoSuchAlgorithmException {
    // Create an unique String (IP Addr + Time + random Value)
    StringBuilder sb = new StringBuilder();

    sb.append(InetAddress.getLocalHost().toString());
    sb.append(System.currentTimeMillis());
    sb.append(System.nanoTime());
    sb.append(Math.random());

    // Calculate digest of the unique string.
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.update(sb.toString().getBytes());

    byte[] digest = md.digest();

    System.out.println("ALGORITHM  : " + md.getAlgorithm());
    System.out.println("KEY LENGTH : " + md.getDigestLength());
    System.out.println(hex.encode(digest));

    return digest;
  }
}

実行結果

ALGORITHM  : MD2
KEY LENGTH : 16
0000: 36 B6 DE 35 19 63 54 13   94 01 BA 10 AC 59 EE 61  6..5.cT......Y.a

ALGORITHM  : MD5
KEY LENGTH : 16
0000: FB 41 AD BB 3D D4 D7 BC   43 62 91 CD 02 B9 BC E7  .A..=...Cb......

ALGORITHM  : SHA-1
KEY LENGTH : 20
0000: 26 DF 65 9F AB 20 09 D0   34 34 F4 7D 11 CC 01 DC  &.e.. ..44......
0010: 8D 03 47 C1

ALGORITHM  : SHA-256
KEY LENGTH : 32
0000: FF D5 42 3C C8 78 8E 50   A1 A5 A0 EE 8A D9 BE 44  ..B<.x.P.......D
0010: 53 1D 6E 63 9B 8B DE CB   34 40 74 C4 24 82 1C 87  S.nc....4@t.$...

ALGORITHM  : SHA-384
KEY LENGTH : 48
0000: 4B 9D E0 A9 0A 56 20 B8   2B 94 5E B6 E3 80 80 22  K....V .+.^...."
0010: DE 26 0B 41 92 61 B1 91   C8 53 4E 67 6B 03 C2 E3  .&.A.a...SNgk...
0020: C2 47 7E 3C F5 07 9B EE   15 0E A9 18 FE 9A 17 84  .G.<............

ALGORITHM  : SHA-512
KEY LENGTH : 64
0000: 85 E7 07 B7 A4 21 6C BE   22 41 62 FB 42 44 CA AC  .....!l."Ab.BD..
0010: BC A0 84 58 87 90 A1 03   62 D9 54 13 68 AB AB FF  ...X....b.T.h...
0020: 40 43 16 66 4A D6 E4 7F   F3 F0 EF 4A 77 A6 14 8F  @C.fJ......Jw...
0030: 18 28 B7 EE 4D CC 94 B2   E1 7E E7 3F 49 ED C6 F9  .(..M......?I...

実行結果もういっちょ

ちゃんと呼び出す毎に Unique な ID が生成されているよってことで

ALGORITHM  : MD2
KEY LENGTH : 16
0000: A7 36 07 D7 68 CB CD CF   55 03 97 E7 12 F8 7F B9  .6..h...U.......

ALGORITHM  : MD5
KEY LENGTH : 16
0000: 33 4F 12 BB BA B4 FA EA   64 95 F8 AA DB 1A BC 5F  3O......d......_

ALGORITHM  : SHA-1
KEY LENGTH : 20
0000: 75 95 17 35 73 9D 97 A1   4C DB EA CE 9D 38 75 D7  u..5s...L....8u.
0010: EC A2 A4 E1
 
ALGORITHM  : SHA-256
KEY LENGTH : 32
0000: 26 C1 70 EA 80 F6 4F F9   A2 60 A3 C2 94 C0 45 14  &.p...O..`....E.
0010: A9 B3 D8 50 72 58 AE 17   45 97 9E 70 21 A7 27 C4  ...PrX..E..p!.'.

ALGORITHM  : SHA-384
KEY LENGTH : 48
0000: DD 15 2A D7 CA AC 12 E7   AC 0D 33 87 49 9B 6A B0  ..*.......3.I.j.
0010: 4C 8A 27 7A DE 6F 18 8D   8A 23 12 E9 C9 E7 8B 12  L.'z.o...#......
0020: 7B 96 C3 2F 3F B5 EE 54   FE 13 EC 3B A2 20 42 93  .../?..T...;.B.

ALGORITHM  : SHA-512
KEY LENGTH : 64
0000: CA BF F4 1C BB A1 47 46   CC 94 FC 8A 4E 99 B0 A6  ......GF....N...
0010: 0C B6 C4 97 A6 F2 75 E0   E6 D1 FE 90 A3 90 1C 39  ......u........9
0020: 26 6F A4 1B 27 49 2D 45   2B CD 6B 19 87 AE 0C 89  &o..'I-E+.k.....
0030: D2 F1 5D 5C 0D 47 F6 97   32 8F 30 F9 E5 97 D5 19  ..]\.G..2.0.....

Java


トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2007-05-31 (木) 02:43:46 (6409d)
Short-URL: http://at-sushi.com/pukiwiki/pukiwiki.php?cmd=s&k=f53dec461c
ISBN10
ISBN13
9784061426061