制作html上第一个页面,我的第一个 HTML 页面

@@ -22,6 +34,36 @@ private EncryptUtils() {

throw new UnsupportedOperationException("u can't fuck me...");

}

+ /**

+ * MD2加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getMD2(String data) {

+ return getMD2(data.getBytes());

+ }

+

+ /**

+ * MD2加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getMD2(byte[] data) {

+ return bytes2HexString(encryptMD2(data));

+ }

+

+ /**

+ * MD2加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptMD2(byte[] data) {

+ return encryptAlgorithm(data, "MD2");

+ }

+

/**

* MD5加密

*

@@ -40,7 +82,7 @@ public static String getMD5(String data) {

* @return 密文

*/

public static String getMD5(String data, String salt) {

- return bytes2Hex(encryptMD5((data + salt).getBytes()));

+ return bytes2HexString(encryptMD5((data + salt).getBytes()));

}

/**

@@ -50,7 +92,7 @@ public static String getMD5(String data, String salt) {

* @return 密文

*/

public static String getMD5(byte[] data) {

- return bytes2Hex(encryptMD5(data));

+ return bytes2HexString(encryptMD5(data));

}

/**

@@ -64,7 +106,7 @@ public static String getMD5(byte[] data, byte[] salt) {

byte[] dataSalt = new byte[data.length + salt.length];

System.arraycopy(data, 0, dataSalt, 0, data.length);

System.arraycopy(salt, 0, dataSalt, data.length, salt.length);

- return bytes2Hex(encryptMD5(dataSalt));

+ return bytes2HexString(encryptMD5(dataSalt));

}

/**

@@ -74,8 +116,169 @@ public static String getMD5(byte[] data, byte[] salt) {

* @return 密文字节数组

*/

public static byte[] encryptMD5(byte[] data) {

+ return encryptAlgorithm(data, "MD5");

+ }

+

+ /**

+ * SHA1加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getSHA1(String data) {

+ return getSHA1(data.getBytes());

+ }

+

+ /**

+ * SHA1加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getSHA1(byte[] data) {

+ return bytes2HexString(encryptSHA1(data));

+ }

+

+ /**

+ * SHA1加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptSHA1(byte[] data) {

+ return encryptAlgorithm(data, "SHA-1");

+ }

+

+ /**

+ * SHA224加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getSHA224(String data) {

+ return getSHA224(data.getBytes());

+ }

+

+ /**

+ * SHA224加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getSHA224(byte[] data) {

+ return bytes2HexString(encryptSHA224(data));

+ }

+

+ /**

+ * SHA224加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptSHA224(byte[] data) {

+ return encryptAlgorithm(data, "SHA-224");

+ }

+

+ /**

+ * SHA256加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getSHA256(String data) {

+ return getSHA256(data.getBytes());

+ }

+

+ /**

+ * SHA256加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getSHA256(byte[] data) {

+ return bytes2HexString(encryptSHA256(data));

+ }

+

+ /**

+ * SHA256加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptSHA256(byte[] data) {

+ return encryptAlgorithm(data, "SHA-256");

+ }

+

+ /**

+ * SHA384加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getSHA384(String data) {

+ return getSHA384(data.getBytes());

+ }

+

+ /**

+ * SHA384加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getSHA384(byte[] data) {

+ return bytes2HexString(encryptSHA384(data));

+ }

+

+ /**

+ * SHA384加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptSHA384(byte[] data) {

+ return encryptAlgorithm(data, "SHA-384");

+ }

+

+ /**

+ * SHA512加密

+ *

+ * @param data 明文字符串

+ * @return 密文

+ */

+ public static String getSHA512(String data) {

+ return getSHA512(data.getBytes());

+ }

+

+ /**

+ * SHA512加密

+ *

+ * @param data 明文字节数组

+ * @return 密文

+ */

+ public static String getSHA512(byte[] data) {

+ return bytes2HexString(encryptSHA512(data));

+ }

+

+ /**

+ * SHA512加密

+ *

+ * @param data 明文字节数组

+ * @return 密文字节数组

+ */

+ public static byte[] encryptSHA512(byte[] data) {

+ return encryptAlgorithm(data, "SHA-512");

+ }

+

+ /**

+ * 对data进行algorithm算法加密

+ *

+ * @param data 明文字节数组

+ * @param algorithm 加密算法

+ * @return 密文字节数组

+ */

+ private static byte[] encryptAlgorithm(byte[] data, String algorithm) {

try {

- MessageDigest md = MessageDigest.getInstance("MD5");

+ MessageDigest md = MessageDigest.getInstance(algorithm);

md.update(data);

return md.digest();

} catch (NoSuchAlgorithmException e) {

@@ -108,7 +311,7 @@ public static String getMD5File(File file) {

MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(buffer);

- return bytes2Hex(md.digest());

+ return bytes2HexString(md.digest());

} catch (NoSuchAlgorithmException | IOException e) {

e.printStackTrace();

} finally {

@@ -122,56 +325,117 @@ public static String getMD5File(File file) {

return "";

}

+

+

+ private static byte[] iv = {1,2,3,4,5,6,7,8};

+ public static String encryptDES(String encryptString, String encryptKey) throws Exception {

+ IvParameterSpec zeroIv = new IvParameterSpec(iv);

+ SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");

+ Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

+ cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

+ byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

+ return new String(encryptedData);

+ }

+ public static String decryptDES(String decryptString, String decryptKey) throws Exception {

+ byte[] byteMi = decryptString.getBytes();

+ IvParameterSpec zeroIv = new IvParameterSpec(iv);

+ SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");

+ Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

+ cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);

+ byte decryptedData[] = cipher.doFinal(byteMi);

+ return new String(decryptedData);

+ }

+

+

+

+

/**

- * SHA加密

+ * 加密

*

- * @param data 明文字符串

- * @return 密文

+ * @param key

+ * 密钥

+ * @param src

+ * 加密文本

+ * @return

+ * @throws Exception

*/

- public static String getSHA(String data) {

- return getSHA(data.getBytes());

+ public static String encrypt(String key, String src) throws Exception {

+ byte[] rawKey = getRawKey(key.getBytes());

+ byte[] result = encrypt(rawKey, src.getBytes());

+ return bytes2HexString(result);

}

/**

- * SHA加密

+ * 解密

*

- * @param data 明文字节数组

- * @return 密文

+ * @param key

+ * 密钥

+ * @param encrypted

+ * 待揭秘文本

+ * @return

+ * @throws Exception

*/

- public static String getSHA(byte[] data) {

- return bytes2Hex(encryptSHA(data));

+ public static String decrypt(String key, String encrypted) throws Exception {

+ byte[] rawKey = getRawKey(key.getBytes());

+ byte[] enc = hexString2Bytes(encrypted);

+ byte[] result = decrypt(rawKey, enc);

+ return new String(result);

}

/**

- * SHA加密

+ * 获取256位的加密密钥

*

- * @param data 明文字节数组

- * @return 密文字节数组

+ * @param seed

+ * @return

+ * @throws Exception

*/

- public static byte[] encryptSHA(byte[] data) {

- try {

- MessageDigest md = MessageDigest.getInstance("SHA");

- md.update(data);

- return md.digest();

- } catch (NoSuchAlgorithmException e) {

- e.printStackTrace();

+ private static byte[] getRawKey(byte[] seed) throws Exception {

+ KeyGenerator kgen = KeyGenerator.getInstance("AES");

+ SecureRandom sr = null;

+ // 在4.2以上版本中,SecureRandom获取方式发生了改变

+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {

+ sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");

+ } else {

+ sr = SecureRandom.getInstance("SHA1PRNG");

}

- return new byte[0];

+ sr.setSeed(seed);

+ // 256 bits or 128 bits,192bits

+ kgen.init(256, sr);

+ SecretKey skey = kgen.generateKey();

+ byte[] raw = skey.getEncoded();

+ return raw;

}

/**

- * 一个byte转为2个hex字符

+ * 真正的加密过程

*

- * @param src byte数组

- * @return 16进制大写字符串

+ * @param key

+ * @param src

+ * @return

+ * @throws Exception

*/

- public static String bytes2Hex(byte[] src) {

- char[] res = new char[src.length << 1];

- final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

- for (int i = 0, j = 0; i < src.length; i++) {

- res[j++] = hexDigits[src[i] >>> 4 & 0x0f];

- res[j++] = hexDigits[src[i] & 0x0f];

- }

- return new String(res);

+ private static byte[] encrypt(byte[] key, byte[] src) throws Exception {

+ SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

+ Cipher cipher = Cipher.getInstance("AES");

+ cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

+ byte[] encrypted = cipher.doFinal(src);

+ return encrypted;

+ }

+

+ /**

+ * 真正的解密过程

+ *

+ * @param key

+ * @param encrypted

+ * @return

+ * @throws Exception

+ */

+ private static byte[] decrypt(byte[] key, byte[] encrypted)

+ throws Exception {

+ SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

+ Cipher cipher = Cipher.getInstance("AES");

+ cipher.init(Cipher.DECRYPT_MODE, skeySpec);

+ byte[] decrypted = cipher.doFinal(encrypted);

+ return decrypted;

}

}

diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

index d487b46495..c42ba54eae 100644

--- a/utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

+++ b/utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

@@ -1,48 +1,48 @@

-package com.blankj.utilcode.utils;

-

-import android.graphics.Bitmap;

-import android.graphics.Bitmap.CompressFormat;

-import android.graphics.BitmapFactory;

-import android.util.Base64;

-

-import java.io.ByteArrayOutputStream;

-

-/**

- * - * author: Blankj

- * blog : http://blankj.com

- * time : 2016/8/12

- * desc :

- *

- */

-public class ImageUtils {

-

- /**

- * 将Bitmap转换成字符串

- *

- * @param bitmap

- * @return

- */

- public static String bitmaptoString(Bitmap bitmap) {

- String string = null;

- ByteArrayOutputStream bStream = new ByteArrayOutputStream();

- bitmap.compress(CompressFormat.PNG, 100, bStream);

- byte[] bytes = bStream.toByteArray();

- string = Base64.encodeToString(bytes, Base64.DEFAULT);

- return string;

- }

-

- /**

- * 把byte数组转化成 bitmap对象

- *

- * @param b

- * @return

- */

- public static Bitmap bytes2Bimap(byte[] b) {

- if (b.length != 0) {

- return BitmapFactory.decodeByteArray(b, 0, b.length);

- } else {

- return null;

- }

- }

-}

+package com.blankj.utilcode.utils;

+

+import android.graphics.Bitmap;

+import android.graphics.Bitmap.CompressFormat;

+import android.graphics.BitmapFactory;

+import android.util.Base64;

+

+import java.io.ByteArrayOutputStream;

+

+/**

+ * + * author: Blankj

+ * blog : http://blankj.com

+ * time : 2016/8/12

+ * desc : 图片相关工具类

+ *

+ */

+public class ImageUtils {

+

+ /**

+ * 将Bitmap转换成字符串

+ *

+ * @param bitmap

+ * @return

+ */

+ public static String bitmaptoString(Bitmap bitmap) {

+ String string = null;

+ ByteArrayOutputStream bStream = new ByteArrayOutputStream();

+ bitmap.compress(CompressFormat.PNG, 100, bStream);

+ byte[] bytes = bStream.toByteArray();

+ string = Base64.encodeToString(bytes, Base64.DEFAULT);

+ return string;

+ }

+

+ /**

+ * 把byte数组转化成 bitmap对象

+ *

+ * @param b

+ * @return

+ */

+ public static Bitmap bytes2Bimap(byte[] b) {

+ if (b.length != 0) {

+ return BitmapFactory.decodeByteArray(b, 0, b.length);

+ } else {

+ return null;

+ }

+ }

+}

diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/UnclassifiedUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/UnclassifiedUtils.java

index d0ab508a33..5ab10e27ea 100644

--- a/utilcode/src/main/java/com/blankj/utilcode/utils/UnclassifiedUtils.java

+++ b/utilcode/src/main/java/com/blankj/utilcode/utils/UnclassifiedUtils.java

@@ -44,21 +44,4 @@ public static boolean isRunningService(Context context, String className) {

}

return false;

}

-

- /**

- * HTML字符转义

- *

对输入参数中的敏感字符进行过滤替换,防止用户利用JavaScript等方式输入恶意代码

- *

- * @param html html文本

- * @return 过滤后的文本

- */

- public static String htmlEscape(String html) {

- return html.replaceAll("&", "&")

- .replaceAll("", ">")

- .replaceAll(" ", " ")

- .replaceAll("'", "'")

- .replaceAll("\"", """)

- .replaceAll("\n", "

");

- }

}

\ No newline at end of file

diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/ConvertUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/ConvertUtilsTest.java

new file mode 100644

index 0000000000..e22d7cf108

--- /dev/null

+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/ConvertUtilsTest.java

@@ -0,0 +1,31 @@

+package com.blankj.utilcode.utils;

+

+

+import org.junit.Test;

+

+import static com.google.common.truth.Truth.assertThat;

+

+

+/**

+ * + * author: Blankj

+ * blog : http://blankj.com

+ * time : 2016/8/13

+ * desc : ConvertUtils单元测试

+ *

+ */

+public class ConvertUtilsTest {

+

+ byte[] mBytes = new byte[]{0x00, 0x08, (byte) 0xdb, 0x33, 0x45, (byte) 0xab, 0x02, 0x23};

+ String hexString = "0008DB3345AB0223";

+

+ @Test

+ public void testBytes2HexString() throws Exception {

+ assertThat(ConvertUtils.bytes2HexString(mBytes)).isEqualTo(hexString);

+ }

+

+ @Test

+ public void testHexString2Bytes() throws Exception {

+ assertThat(ConvertUtils.hexString2Bytes(hexString)).isEqualTo(mBytes);

+ }

+}

\ No newline at end of file

diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/EncodeUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/EncodeUtilsTest.java

index 2e3bfc4964..6cf518455d 100644

--- a/utilcode/src/test/java/com/blankj/utilcode/utils/EncodeUtilsTest.java

+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/EncodeUtilsTest.java

@@ -1,65 +1,66 @@

-package com.blankj.utilcode.utils;

-

-import org.junit.Test;

-import org.junit.runner.RunWith;

-import org.robolectric.RobolectricTestRunner;

-import org.robolectric.annotation.Config;

-

-import static com.google.common.truth.Truth.assertThat;

-

-/**

- * - * author: Blankj

- * blog : http://blankj.com

- * time : 2016/8/12

- * desc : EncodeUtils单元测试

- *

- */

-@RunWith(RobolectricTestRunner.class)

-@Config(manifest = Config.NONE)

-public class EncodeUtilsTest {

-

- String urlEncodeString = "%E5%93%88%E5%93%88%E5%93%88";

- String html = "" +

- "" +

- "我的第一个 HTML 页面" +

- "" +

- "" +

- "

body 元素的内容会显示在浏览器中。" +

- "

title 元素的内容会显示在浏览器的标题栏中。" +

- "" +

- "";

- String encodeHtml = "

我的第一个 HTML 页面

body 元素的内容会显示在浏览器中。

title 元素的内容会显示在浏览器的标题栏中。

";

-

- @Test

- public void testUrlEncode() throws Exception {

- assertThat(EncodeUtils.urlEncode("哈哈哈")).isEqualTo(urlEncodeString);

- assertThat(EncodeUtils.urlEncode("哈哈哈", "UTF-8")).isEqualTo(urlEncodeString);

- }

-

- @Test

- public void testUrlDecode() throws Exception {

- assertThat(EncodeUtils.urlDecode(urlEncodeString)).isEqualTo("哈哈哈");

- assertThat(EncodeUtils.urlDecode(urlEncodeString, "UTF-8")).isEqualTo("哈哈哈");

- }

-

- @Test

- public void testBase64Encode() throws Exception {

- assertThat(EncodeUtils.base64Encode("blankj")).isEqualTo("Ymxhbmtq");

- }

-

- @Test

- public void testBase64Decode() throws Exception {

- assertThat(EncodeUtils.base64Decode("Ymxhbmtq")).isEqualTo("blankj");

- }

-

- @Test

- public void testHtmlEncode() throws Exception {

- assertThat(EncodeUtils.htmlEncode(html)).isEqualTo(encodeHtml);

- }

-

- @Test

- public void testHtmlDecode() throws Exception {

- assertThat(EncodeUtils.htmlDecode(encodeHtml)).isEqualTo(html);

- }

+package com.blankj.utilcode.utils;

+

+import org.junit.Test;

+import org.junit.runner.RunWith;

+import org.robolectric.RobolectricTestRunner;

+import org.robolectric.annotation.Config;

+

+import static com.blankj.utilcode.utils.EncodeUtils.*;

+import static com.google.common.truth.Truth.assertThat;

+

+/**

+ * + * author: Blankj

+ * blog : http://blankj.com

+ * time : 2016/8/12

+ * desc : EncodeUtils单元测试

+ *

+ */

+@RunWith(RobolectricTestRunner.class)

+@Config(manifest = Config.NONE)

+public class EncodeUtilsTest {

+

+ String urlEncodeString = "%E5%93%88%E5%93%88%E5%93%88";

+ String html = "" +

+ "" +

+ "我的第一个 HTML 页面" +

+ "" +

+ "" +

+ "

body 元素的内容会显示在浏览器中。" +

+ "

title 元素的内容会显示在浏览器的标题栏中。" +

+ "" +

+ "";

+ String encodeHtml = "

我的第一个 HTML 页面

body 元素的内容会显示在浏览器中。

title 元素的内容会显示在浏览器的标题栏中。

";

+

+ @Test

+ public void testUrlEncode() throws Exception {

+ assertThat(urlEncode("哈哈哈")).isEqualTo(urlEncodeString);

+ assertThat(urlEncode("哈哈哈", "UTF-8")).isEqualTo(urlEncodeString);

+ }

+

+ @Test

+ public void testUrlDecode() throws Exception {

+ assertThat(urlDecode(urlEncodeString)).isEqualTo("哈哈哈");

+ assertThat(urlDecode(urlEncodeString, "UTF-8")).isEqualTo("哈哈哈");

+ }

+

+ @Test

+ public void testBase64Encode() throws Exception {

+ assertThat(base64Encode("blankj")).isEqualTo("Ymxhbmtq");

+ }

+

+ @Test

+ public void testBase64Decode() throws Exception {

+ assertThat(base64Decode("Ymxhbmtq")).isEqualTo("blankj");

+ }

+

+ @Test

+ public void testHtmlEncode() throws Exception {

+ assertThat(htmlEncode(html)).isEqualTo(encodeHtml);

+ }

+

+ @Test

+ public void testHtmlDecode() throws Exception {

+ assertThat(htmlDecode(encodeHtml)).isEqualTo(html);

+ }

}

\ No newline at end of file

diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java

index e14704aec9..7421780a57 100644

--- a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java

+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java

@@ -1,7 +1,12 @@

package com.blankj.utilcode.utils;

import org.junit.Test;

+import org.junit.runner.RunWith;

+import org.robolectric.RobolectricTestRunner;

+import org.robolectric.annotation.Config;

+import static com.blankj.utilcode.utils.ConvertUtils.*;

+import static com.blankj.utilcode.utils.EncryptUtils.*;

import static com.google.common.truth.Truth.assertThat;

/**

@@ -12,47 +17,130 @@

* desc : EncryptUtils单元测试

*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值