SaltUtil.java 887 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package com.dk.oauth.util;
  2. import org.apache.commons.codec.digest.DigestUtils;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.shiro.crypto.SecureRandomNumberGenerator;
  5. /**
  6. * 盐值包装工具类
  7. */
  8. public class SaltUtil {
  9. /**
  10. * 盐值包装
  11. *
  12. * @param secret 配置文件中配置的附加盐值
  13. * @param salt 数据库中保存的盐值
  14. * @return
  15. */
  16. public static String getSalt(String secret, String salt) {
  17. if (StringUtils.isBlank(secret) && StringUtils.isBlank(salt)) {
  18. return null;
  19. }
  20. // 加密方法
  21. String newSalt = DigestUtils.sha256Hex(secret + salt);
  22. return newSalt;
  23. }
  24. /**
  25. * 生成32位随机盐
  26. *
  27. * @return
  28. */
  29. public static String generateSalt() {
  30. return new SecureRandomNumberGenerator().nextBytes(16).toHex();
  31. }
  32. }