关于token,有点疑惑。。

提问 1 888
nalaismo1
nalaismo1 2021-10-06
版本:renren-fast 开发环境:
根据代码,TokenGenerator中利用了UUID,并对其MD5哈希,然后作为token值返回。 [pre] public class TokenGenerator { public static String generateValue() { return generateValue(UUID.randomUUID().toString()); } private static final char[] hexCode = "0123456789abcdef".toCharArray(); public static String toHexString(byte[] data) { if(data == null) { return null; } StringBuilder r = new StringBuilder(data.length*2); for ( byte b : data) { r.append(hexCode[(b >> 4) & 0xF]); r.append(hexCode[(b & 0xF)]); } return r.toString(); } public static String generateValue(String param) { try { MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(param.getBytes()); byte[] messageDigest = algorithm.digest(); return toHexString(messageDigest); } catch (Exception e) { throw new RRException("生成Token失败", e); } } } [/pre] 这里不明白,为什么不直接返回UUID?难道token不是只需要唯一就行了吗?MD5哈希的作用体现在哪里?
回帖
  • 这块代码是参考jwt的源码,也可以直接使用uuid
    0 回复