Skip to content

Commit

Permalink
MD5加密问题,2进制与16进制转换问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fengwenyi committed Jan 24, 2019
1 parent fd548fc commit 9e08ab3
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.fengwenyi</groupId>
<artifactId>JavaLib</artifactId>
<version>1.0.5.2.1.SNAPSHOT</version>
<version>1.0.5.3.SNAPSHOT</version>
<!--<version>1.0.5.RELEASE</version>-->

<name>JavaLib</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fengwenyi.javalib.handler;

import com.fengwenyi.javalib.util.StringUtil;
import com.fengwenyi.javalib.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
Expand All @@ -26,7 +26,7 @@ public class HandlerRegister {
* @return handler
*/
public Handler getHandler(String key) {
if (StringUtil.isEmpty(key) // key为空
if (StringUtils.isEmpty(key) // key为空
|| handlers == null // handlers没有被实例化
|| handlers.isEmpty()) // handlers没有值
return null;
Expand All @@ -41,7 +41,7 @@ public Handler getHandler(String key) {
public void setHandler(String key, Handler handler) {

// 参数判断
if (StringUtil.isEmpty(key) // key 为空
if (StringUtils.isEmpty(key) // key 为空
|| handler == null) // handler 没有被实例化
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fengwenyi.javalib.messageengine;

import com.fengwenyi.javalib.util.StringUtil;
import com.fengwenyi.javalib.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,7 +27,7 @@ public class CommonMessage {
* @param value map-value
*/
public void setHeader(String key, Object value) {
if (StringUtil.isNotEmpty(key))
if (StringUtils.isNotEmpty(key))
header.put(key, value);
}

Expand All @@ -38,7 +38,7 @@ public void setHeader(String key, Object value) {
* 如果key为空或是header为空,都将返回null
*/
public Object getHeader(String key) {
if (StringUtil.isEmpty(key)
if (StringUtils.isEmpty(key)
|| header == null
|| header.isEmpty())
return null;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/fengwenyi/javalib/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

Expand Down Expand Up @@ -39,7 +38,7 @@ public class DateTimeUtil {
*/
public static Date stringToDate(String source, String format) throws ParseException {

if (StringUtil.isEmpty(source))
if (StringUtils.isEmpty(source))
return null;

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void notNull(Object object, String message) {
* 但是你仍然坚持要为空,那你可以添加一个空格(" ")试试
* */
if (object instanceof String)
if (StringUtil.isEmpty((String) object))
if (StringUtils.isEmpty((String) object))
throw new IllegalArgumentException(message);
}

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/fengwenyi/javalib/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import org.springframework.beans.BeanUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 文件工具类
Expand Down Expand Up @@ -74,7 +71,7 @@ private String read(String path) throws IOException {
*/
public static FileBean getFileInfo(String file) throws IOException {

if (StringUtil.isEmpty(file)) {
if (StringUtils.isEmpty(file)) {
ExceptionUtil.notNull(file);
return null;
}
Expand Down Expand Up @@ -120,7 +117,7 @@ public static FileBean getFileInfo(String file) throws IOException {
*/
public static VideoBean getVideoInfo(String file) throws IOException, EncoderException {

if (StringUtil.isEmpty(file)) {
if (StringUtils.isEmpty(file)) {
ExceptionUtil.notNull(file, "file must not null");
return null;
}
Expand Down Expand Up @@ -159,7 +156,7 @@ public static VideoBean getVideoInfo(String file) throws IOException, EncoderExc
*/
public static AudioBean getAudioInfo(String file) throws IOException, EncoderException {

if (StringUtil.isEmpty(file)) {
if (StringUtils.isEmpty(file)) {
ExceptionUtil.notNull(file, "file must not null");
return null;
}
Expand Down Expand Up @@ -194,7 +191,7 @@ public static AudioBean getAudioInfo(String file) throws IOException, EncoderExc
*/
public static ImageBean getImageInfo(String file) throws IOException, EncoderException {

if (StringUtil.isEmpty(file)) {
if (StringUtils.isEmpty(file)) {
ExceptionUtil.notNull(file, "file must not null");
return null;
}
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/com/fengwenyi/javalib/util/HexUtil.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/main/java/com/fengwenyi/javalib/util/HexUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.fengwenyi.javalib.util;

/**
* 进制转换工具类
* @author Wenyi Feng.
*/
public class HexUtils {

/**
* 二进制转十六进制
* @param bytes [ellipsis]
* @return [ellipsis]
*/
public static String _2_16(byte[] bytes) {

if (bytes == null || bytes.length == 0)
return null;

StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
String hex = Integer.toHexString(aByte & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}


/**
* 16进制转化为 2进制
* @param hexStr 16进制字符串
* @return byte[]
*/
public static byte[] _16_2(String hexStr) {
if (StringUtils.isEmpty(hexStr))
return null;

byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}

}
39 changes: 34 additions & 5 deletions src/main/java/com/fengwenyi/javalib/util/MD5Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
public class MD5Util {

/**
* 32位大写
* @param plainText [ellipsis]
* @return [ellipsis]
* @throws NoSuchAlgorithmException [ellipsis]
* MD5加密
* @param plainText 待加密的字符串
* @return MD5加密之后的字符串,32位小写
* @throws NoSuchAlgorithmException 算法失败
*/
public static String MD5(String plainText) throws NoSuchAlgorithmException {
// 创建一个md5算法对象
Expand All @@ -25,7 +25,36 @@ public static String MD5(String plainText) throws NoSuchAlgorithmException {
// 获得MD5字节数组,16*8=128位
byte[] md5Byte = md.digest(messageByte);
// 转换为16进制字符串
return HexUtil._2_16(md5Byte);

StringBuilder sb = new StringBuilder();
// 把每一个byte 做一个与运算 0xff;
for (byte b : md5Byte) {
// 与运算
int number = b & 0xff;// 加盐
String str = Integer.toHexString(number);
if (str.length() == 1) {
sb.append("0");
}
sb.append(str);
}
//32位加密
return sb.toString();
}

/**
* 加密解密算法 执行一次加密,两次解密
* @param str 待加密字符串/加密后的字符串
* @return 加密后的字符串/解密后的字符串
*/
public static String convertMD5(String str){

char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ 't');
}
return new String(a);

}


}
2 changes: 1 addition & 1 deletion src/main/java/com/fengwenyi/javalib/util/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static String getUrlParamsByMap(Map<String, String> data) throws Unsuppor
*/
public static Map<String, String> getUrlParams(String param) {
Map<String, String> map = new HashMap<String, String>();
if (StringUtil.isEmpty(param)) {
if (StringUtils.isEmpty(param)) {
return map;
}
String[] params = param.split("&");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fengwenyi/javalib/util/RSAUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static String sign(String content, String algorithm, String privateKey, S
Signature signTool = Signature.getInstance(algorithm);
PrivateKey key = commonGetPrivatekeyByText(privateKey);
signTool.initSign(key);
if (StringUtil.isEmpty(charset)) {
if (StringUtils.isEmpty(charset)) {
signTool.update(content.getBytes());
} else {
signTool.update(content.getBytes(charset));
Expand Down Expand Up @@ -236,7 +236,7 @@ public static boolean verify(String content, String signature, String algorithm,
Signature signTool = Signature.getInstance(algorithm);
PublicKey key = commonGetPublickeyByText(publicKey);
signTool.initVerify(key);
if (StringUtil.isEmpty(charset)) {
if (StringUtils.isEmpty(charset)) {
signTool.update(content.getBytes());
} else {
signTool.update(content.getBytes(charset));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fengwenyi/javalib/util/SHAUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static String SHA1(String plainText) throws NoSuchAlgorithmException {
// 返回值
String result = null;
// 是否是有效字符串
if (StringUtil.isNotEmpty(plainText)) {
if (StringUtils.isNotEmpty(plainText)) {
// SHA 加密开始
// 创建加密对象 并传入加密类型
MessageDigest messageDigest = MessageDigest.getInstance(EncryptionType.SHA1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* </ul>
* @author Wenyi Feng
*/
public class StringUtil {
public class StringUtils {

/**
* 判断字符串是否为空
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/fengwenyi/test/MD5Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.fengwenyi.test;

import com.fengwenyi.javalib.util.MD5Util;
import org.junit.Test;

import java.security.NoSuchAlgorithmException;

/**
* @author Wenyi Feng
* @since 2019-01-24
*/
public class MD5Test {

@Test
public void md5() {
String md5Str = null;
try {
md5Str = MD5Util.MD5("12345678");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println(md5Str);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.fengwenyi.test;

import com.fengwenyi.javalib.util.StringUtil;
import com.fengwenyi.javalib.util.StringUtils;
import org.junit.Test;

/**
* @author Wenyi Feng
* @since 2018-11-13
*/
public class StringUtilTest {
public class StringUtilsTest {

@Test
public void testAutoGenericCode() {
long startTime = System.nanoTime();
String rs = StringUtil.autoFill(11, 10);
String rs = StringUtils.autoFill(11, 10);
System.out.println(rs);
long endTime = System.nanoTime();
long time = endTime - startTime;
Expand All @@ -22,7 +22,7 @@ public void testAutoGenericCode() {
/*@Test
public void testFillStr() {
long startTime = System.nanoTime();
String rs = StringUtil.autoFill(11, 10000, "0");
String rs = StringUtils.autoFill(11, 10000, "0");
System.out.println(rs);
long endTime = System.nanoTime();
long time = endTime - startTime;
Expand All @@ -31,13 +31,13 @@ public void testFillStr() {

@Test
public void testFill() {
String rs = StringUtil.autoFill( "Dd", 10, null, false);
String rs = StringUtils.autoFill( "Dd", 10, null, false);
System.out.println(rs);
}

@Test
public void testHasNum() {
boolean rs = StringUtil.hasOnlyNum("90000001");
boolean rs = StringUtils.hasOnlyNum("90000001");
System.out.println(rs);
}

Expand Down

0 comments on commit 9e08ab3

Please sign in to comment.