Blame view

src/main/java/com/objecteye/utils/DigestUtils.java 3.09 KB
020ea372   Hugiee.Liu   fea: 测试接口添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  package com.objecteye.utils;
  
  import org.apache.commons.codec.binary.Hex;
  
  import java.security.GeneralSecurityException;
  import java.security.MessageDigest;
  import java.util.Random;
  
  public class DigestUtils {
  
  
      /**
       * 加密遵循RFC2671规范 将相关参数加密生成一个MD5字符串,并返回
       */
      public static String getResponse(
              String username,
              String realm,
              String password,
              String nonce,
              String nc,
              String cnonce,
              String qop,
              String method,
              String uri
      ) {
          String A1 = username + ":" + realm + ":" + password;
          byte[] md5ByteA1 = md5(A1.getBytes());
          String HA1 = new String(Hex.encodeHex(md5ByteA1));
  
          String A2 = method + ":" + uri;
          byte[] md5ByteA2 = md5(A2.getBytes());
          String HA2 = new String(Hex.encodeHex(md5ByteA2));
  
  
          String original = HA1 + ":" + (nonce + ":" + nc + ":" + cnonce + ":" + qop) + ":" + HA2;
          byte[] md5ByteOriginal = md5(original.getBytes());
          return new String(Hex.encodeHex(md5ByteOriginal));
      }
  
      public static String getAuthorization(String username, String realm, String nonce, String uri, String qop, String nc, String cnonce, String response, String opaque) {
          return "Digest username=\"" + username + "\"" +
                  ",realm=\"" + realm + "\"" +
                  ",nonce=\"" + nonce + "\"" +
                  ",uri=\"" + uri + "\"" +
                  ",qop=\"" + qop + "\"" +
                  ",nc=\"" + nc + "\"" +
                  ",cnonce=\"" + cnonce + "\"" +
                  ",response=\"" + response + "\"" +
                  ",opaque=\"" + opaque;
      }
  
  
      /**
       * 对输入字符串进行md5散列.
       */
      public static byte[] md5(byte[] input) {
          return digest(input, "MD5", null, 1);
      }
  
      /**
       * 对字符串进行散列, 支持md5sha1算法.
       */
      private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
          try {
              MessageDigest digest = MessageDigest.getInstance(algorithm);
  
              if (salt != null) {
                  digest.update(salt);
              }
  
              byte[] result = digest.digest(input);
  
              for (int i = 1; i < iterations; i++) {
                  digest.reset();
                  result = digest.digest(result);
              }
              return result;
          } catch (GeneralSecurityException e) {
              throw new RuntimeException(e);
          }
      }
  
      public static String generateSalt2(int length) {
          StringBuilder val = new StringBuilder();
          Random random = new Random();
          //参数length,表示生成几位随机数
          for (int i = 0; i < length; i++) {
              //输出字母还是数字
              if (random.nextInt(2) % 2 == 0) {
                  //输出是大写字母还是小写字母
                  val.append((char) (random.nextInt(26) + random.nextInt(2) % 2 == 0 ? 65 : 97));
              } else {
                  val.append(random.nextInt(10));
              }
          }
          return val.toString().toLowerCase();
      }
  
  
  }