PageRenderTime 7400ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/shiro-example-chapter5/src/test/java/com/github/zhangkaitao/shiro/chapter5/hash/CodecAndCryptoTest.java

https://gitlab.com/0072016/0072016-show
Java | 203 lines | 132 code | 53 blank | 18 comment | 0 complexity | 57cfaf64ac40a316c1e3628d8cab1836 MD5 | raw file
  1. package com.github.zhangkaitao.shiro.chapter5.hash;
  2. import junit.framework.Assert;
  3. import org.apache.shiro.codec.Base64;
  4. import org.apache.shiro.codec.CodecSupport;
  5. import org.apache.shiro.codec.Hex;
  6. import org.apache.shiro.crypto.*;
  7. import org.apache.shiro.crypto.hash.*;
  8. import org.apache.shiro.util.ByteSource;
  9. import org.apache.shiro.util.SimpleByteSource;
  10. import org.junit.Test;
  11. import javax.crypto.Cipher;
  12. import java.security.*;
  13. import java.security.interfaces.RSAPrivateKey;
  14. import java.security.spec.EncodedKeySpec;
  15. import java.security.spec.PKCS8EncodedKeySpec;
  16. import java.security.spec.RSAPrivateKeySpec;
  17. import java.security.spec.X509EncodedKeySpec;
  18. /**
  19. * <p>User: Zhang Kaitao
  20. * <p>Date: 14-1-27
  21. * <p>Version: 1.0
  22. */
  23. public class CodecAndCryptoTest {
  24. @Test
  25. public void testBase64() {
  26. String str = "hello";
  27. String base64Encoded = Base64.encodeToString(str.getBytes());
  28. String str2 = Base64.decodeToString(base64Encoded);
  29. Assert.assertEquals(str, str2);
  30. }
  31. @Test
  32. public void testHex() {
  33. String str = "hello";
  34. String base64Encoded = Hex.encodeToString(str.getBytes());
  35. String str2 = new String(Hex.decode(base64Encoded.getBytes()));
  36. Assert.assertEquals(str, str2);
  37. }
  38. @Test
  39. public void testCodecSupport() {
  40. String str = "hello";
  41. byte[] bytes = CodecSupport.toBytes(str, "utf-8");
  42. String str2 = CodecSupport.toString(bytes, "utf-8");
  43. Assert.assertEquals(str, str2);
  44. }
  45. @Test
  46. public void testRandom() {
  47. //生成随机数
  48. SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
  49. randomNumberGenerator.setSeed("123".getBytes());
  50. System.out.println(randomNumberGenerator.nextBytes().toHex());
  51. }
  52. @Test
  53. public void testMd5() {
  54. String str = "hello";
  55. String salt = "123";
  56. String md5 = new Md5Hash(str, salt).toString();//还可以转换为 toBase64()/toHex()
  57. System.out.println(md5);
  58. }
  59. @Test
  60. public void testSha1() {
  61. String str = "hello";
  62. String salt = "123";
  63. String sha1 = new Sha1Hash(str, salt).toString();
  64. System.out.println(sha1);
  65. }
  66. @Test
  67. public void testSha256() {
  68. String str = "hello";
  69. String salt = "123";
  70. String sha1 = new Sha256Hash(str, salt).toString();
  71. System.out.println(sha1);
  72. }
  73. @Test
  74. public void testSha384() {
  75. String str = "hello";
  76. String salt = "123";
  77. String sha1 = new Sha384Hash(str, salt).toString();
  78. System.out.println(sha1);
  79. }
  80. @Test
  81. public void testSha512() {
  82. String str = "hello";
  83. String salt = "123";
  84. String sha1 = new Sha512Hash(str, salt).toString();
  85. System.out.println(sha1);
  86. }
  87. @Test
  88. public void testSimpleHash() {
  89. String str = "hello";
  90. String salt = "123";
  91. //MessageDigest
  92. String simpleHash = new SimpleHash("SHA-1", str, salt).toString();
  93. System.out.println(simpleHash);
  94. }
  95. @Test
  96. public void testHashService() {
  97. DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
  98. hashService.setHashAlgorithmName("SHA-512");
  99. hashService.setPrivateSalt(new SimpleByteSource("123")); //私盐,默认无
  100. hashService.setGeneratePublicSalt(true);//是否生成公盐,默认false
  101. hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
  102. hashService.setHashIterations(1); //生成Hash值的迭代次数
  103. HashRequest request = new HashRequest.Builder()
  104. .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes("hello"))
  105. .setSalt(ByteSource.Util.bytes("123")).setIterations(2).build();
  106. String hex = hashService.computeHash(request).toHex();
  107. System.out.println(hex);
  108. }
  109. @Test
  110. public void testAesCipherService() {
  111. AesCipherService aesCipherService = new AesCipherService();
  112. aesCipherService.setKeySize(128);//设置key长度
  113. //生成key
  114. Key key = aesCipherService.generateNewKey();
  115. String text = "hello";
  116. //加密
  117. String encrptText = aesCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
  118. //解密
  119. String text2 = new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
  120. Assert.assertEquals(text, text2);
  121. }
  122. @Test
  123. public void testBlowfishCipherService() {
  124. BlowfishCipherService blowfishCipherService = new BlowfishCipherService();
  125. blowfishCipherService.setKeySize(128);
  126. //生成key
  127. Key key = blowfishCipherService.generateNewKey();
  128. String text = "hello";
  129. //加密
  130. String encrptText = blowfishCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
  131. //解密
  132. String text2 = new String(blowfishCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
  133. Assert.assertEquals(text, text2);
  134. }
  135. @Test
  136. public void testDefaultBlockCipherService() throws Exception {
  137. //对称加密,使用Java的JCA(javax.crypto.Cipher)加密API,常见的如 'AES', 'Blowfish'
  138. DefaultBlockCipherService cipherService = new DefaultBlockCipherService("AES");
  139. cipherService.setKeySize(128);
  140. //生成key
  141. Key key = cipherService.generateNewKey();
  142. String text = "hello";
  143. //加密
  144. String encrptText = cipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
  145. //解密
  146. String text2 = new String(cipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
  147. Assert.assertEquals(text, text2);
  148. }
  149. //加密/解密相关知识可参考snowolf的博客 http://snowolf.iteye.com/category/68576
  150. }