dev
rekoe 2020-01-12 10:01:24 +08:00
parent 2cf576c18b
commit 04f425bb71
8 changed files with 403 additions and 146 deletions

View File

@ -1,5 +0,0 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/main/resources/msg/zh-CN/MessageResources.properties=UTF-8
encoding/<project>=UTF-8

View File

@ -1,9 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
#Sun Jan 12 10:00:36 CST 2020
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.compliance=1.8

View File

@ -1,4 +0,0 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

31
pom.xml
View File

@ -27,21 +27,21 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.7.6</version>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-plugins-slog</artifactId>
<version>${nutz.version}</version>
</dependency>
<dependency>
<groupId>com.restfb</groupId>
<artifactId>restfb</artifactId>
<version>1.6.14</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
@ -58,7 +58,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<version>20190722</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
@ -99,7 +99,7 @@
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
@ -121,21 +121,6 @@
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.brickred</groupId>
<artifactId>socialauth</artifactId>
<version>4.5</version>
<exclusions>
<exclusion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</exclusion>
<exclusion>
<groupId>org.openid4java</groupId>
<artifactId>openid4java-consumer</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

View File

@ -61,11 +61,9 @@ public class RepositoryService {
/**
* svn
*
* @param pjId
* ID
* @param pjId ID
* @return svn
* @throws SVNException
* svn
* @throws SVNException svn
*/
public SVNRepository getRepository(String pjId) throws SVNException {
Pj pj = projectService.fetch(Cnd.where("pj", "=", pjId));
@ -79,8 +77,7 @@ public class RepositoryService {
/**
* urlsvnurl
*
* @param url
* url
* @param url url
* @return svn url
*/
public static String parseURL(String url) {
@ -106,11 +103,9 @@ public class RepositoryService {
/**
* svn
*
* @param pj
*
* @param pj
* @return svn
* @throws SVNException
* svn
* @throws SVNException svn
*/
@SuppressWarnings("deprecation")
public SVNRepository getRepository(Pj pj) throws SVNException {
@ -185,28 +180,28 @@ public class RepositoryService {
return true;
}
public static void main(String[] args) {
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
public static void main(String[] args) throws SVNException {
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("admin", "123456");
SVNClientManager manager = SVNClientManager.newInstance();
manager.setAuthenticationManager(authManager);
SVNCommitClient commitClient = SVNClientManager.newInstance().getCommitClient();
SVNClientManager manager = SVNClientManager.newInstance(SVNWCUtil.createDefaultOptions(true), authManager);
SVNCommitClient commitClient = manager.getCommitClient();
commitClient.getDebugLog();
try {
SVNCommitInfo info = commitClient.doMkDir(new SVNURL[] { SVNURL.parseURIEncoded("http://127.0.0.1:3380/svn/hunnit") }, "commitMessage", null, true);
SVNCommitInfo info = commitClient.doMkDir(new SVNURL[] { SVNURL.parseURIEncoded("http://svn.test.com/svn/hunnit/test") }, "commitMessage", null, true);
long newRevision = info.getNewRevision();
System.out.println(newRevision);
} catch (SVNException e) {
} catch (Exception e) {
e.printStackTrace();
}
SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded("http://svn.test.com/svn/hunnit"));
repository.setAuthenticationManager(authManager);
long lastVersion = repository.getLatestRevision();
System.out.println(lastVersion);
}
/**
*
*
* @param pj
*
* @param pj
* @return
*/
public String getRepositoryRoot(Pj pj) {
@ -230,10 +225,8 @@ public class RepositoryService {
/**
* svn
*
* @param pj
*
* @param path
*
* @param pj
* @param path
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@ -268,8 +261,7 @@ public class RepositoryService {
* <code>createLocalRepository(path, null, enableRevisionProperties, force)</code>
* .
*
* @param respository
* a repository root location
* @param respository a repository root location
* @return a local URL (file:///) of a newly created repository
*/
public static SVNURL createLocalRepository(File respository) {

View File

@ -17,6 +17,7 @@ import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.repo.Base64;
import com.rekoe.domain.Pj;
import com.rekoe.domain.PjAuth;
@ -25,6 +26,9 @@ import com.rekoe.domain.Usr;
import com.rekoe.utils.Constants;
import com.rekoe.utils.EncryptUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
/**
* svn
*
@ -62,8 +66,7 @@ public class SvnService {
/**
*
*
* @param pj
* id
* @param pj id
*/
public synchronized void exportConfig(String pj) {
this.exportConfig(projectService.fetch(Cnd.where("pj", "=", pj)));
@ -72,8 +75,7 @@ public class SvnService {
/**
*
*
* @param pj
*
* @param pj
*/
public synchronized void exportConfig(Pj pj) {
if (pj == null) {
@ -100,8 +102,7 @@ public class SvnService {
/**
* svn
*
* @param pj
*
* @param pj
*/
private void exportSVN(Pj pj) {
// 项目的用户
@ -118,8 +119,7 @@ public class SvnService {
/**
* http()
*
* @param pj
*
* @param pj
*/
private void exportHTTP(Pj pj) {
// 项目的用户
@ -136,8 +136,7 @@ public class SvnService {
/**
* http()
*
* @param root
* svn root
* @param root svn root
*/
private void exportHTTPMutil(File root) {
String svnRoot = StringUtils.replace(root.getAbsolutePath(), "\\", "/");
@ -158,8 +157,7 @@ public class SvnService {
/**
* svn root
*
* @param rootPath
* svn root
* @param rootPath svn root
* @return svn root
*/
private Map<String, List<PjAuth>> getPjAuthsByRootPath(String rootPath) {
@ -180,8 +178,7 @@ public class SvnService {
/**
*
*
* @param pj
*
* @param pj
* @return
*/
private Map<String, List<PjAuth>> getPjAuths(String pj) {
@ -203,8 +200,7 @@ public class SvnService {
/**
*
*
* @param pj
*
* @param pj
* @return
*/
private Map<String, List<PjGrUsr>> getPjGrUsrs(String pj) {
@ -225,8 +221,7 @@ public class SvnService {
/**
* svn root
*
* @param rootPath
* svn root
* @param rootPath svn root
* @return svn root
*/
private Map<String, List<PjGrUsr>> getPjGrUsrsByRootPath(String rootPath) {
@ -250,10 +245,8 @@ public class SvnService {
/**
* http
*
* @param root
* svn root
* @param usrList
*
* @param root svn root
* @param usrList
*/
private void exportPasswdHTTPMutil(File root, List<Usr> usrList) {
File outFile = new File(root, "passwd.http");
@ -270,10 +263,8 @@ public class SvnService {
/**
* http
*
* @param pj
*
* @param usrList
*
* @param pj
* @param usrList
*/
private void exportPasswdHTTP(Pj pj, List<Usr> usrList) {
String path = projectConfigService.getRepoPath(pj);
@ -291,10 +282,8 @@ public class SvnService {
/**
* svn
*
* @param pj
*
* @param usrList
*
* @param pj
* @param usrList
*/
private void exportPasswdSVN(Pj pj, List<Usr> usrList) {
String path = projectConfigService.getRepoPath(pj);
@ -310,12 +299,9 @@ public class SvnService {
/**
* http
*
* @param root
* svn root
* @param pjGrUsrMap
*
* @param resMap
*
* @param root svn root
* @param pjGrUsrMap
* @param resMap
*/
private void exportAuthzHTTPMutil(File root, Map<String, List<PjGrUsr>> pjGrUsrMap, Map<String, List<PjAuth>> resMap) {
if (root == null) {
@ -362,20 +348,17 @@ public class SvnService {
/**
*
*
* @param pj
*
* @param pjGrUsrMap
*
* @param resMap
*
* @param pj
* @param pjGrUsrMap
* @param resMap
*/
private void exportAuthz(Pj pj, Map<String, List<PjGrUsr>> pjGrUsrMap, Map<String, List<PjAuth>> resMap) {
if (pj == null || StringUtils.isBlank(pj.getPj())) {
return;
}
/*
* if(pjGrList == null || pjGrList.size() == 0){ return; } if(pjAuthMap
* == null || pjAuthMap.size() == 0){ return; }
* if(pjGrList == null || pjGrList.size() == 0){ return; } if(pjAuthMap == null
* || pjAuthMap.size() == 0){ return; }
*/
String path = projectConfigService.getRepoPath(pj);
File outFile = new File(path, "/conf/authz");
@ -418,8 +401,7 @@ public class SvnService {
/**
* svnsvnserve.conf
*
* @param pj
*
* @param pj
*/
private void exportSvnConf(Pj pj) {
if (pj == null || StringUtils.isBlank(pj.getPj())) {
@ -440,8 +422,7 @@ public class SvnService {
/**
* httphttpd.conf
*
* @param pj
*
* @param pj
*/
private void exportSVNPathConf(Pj pj) {
if (pj == null || StringUtils.isBlank(pj.getPj())) {
@ -474,8 +455,7 @@ public class SvnService {
/**
* httphttpd.conf
*
* @param root
* svn root
* @param root svn root
*/
private void exportSVNParentPathConf(File root) {
String svnRoot = StringUtils.replace(root.getAbsolutePath(), "\\", "/");
@ -500,10 +480,8 @@ public class SvnService {
/**
*
*
* @param outFile
*
* @param contents
*
* @param outFile
* @param contents
*/
private void write(File outFile, String contents) {
BufferedWriter writer = null;
@ -535,4 +513,13 @@ public class SvnService {
}
}
}
public static void main(String[] args) {
String key = "admin" + ":{SHA}" + Base64.encodeToString(DigestUtil.digester(DigestAlgorithm.SHA1).digest("123456"), false);
System.out.println(key);
String key2 = "admin" + ":{SHA}" + EncryptUtil.encriptSHA1("123456");
System.out.println(key2);
String decode = EncryptUtil.decrypt("fEqNCco3Yq9h5ZUglD3CZJT4lBs=");
System.out.println(decode);
}
}

View File

@ -0,0 +1,325 @@
package com.rekoe.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.ArrayUtils;
import org.nutz.repo.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* AES RSA Hex
*/
public class CryptTest {
private static final String UTF8 = "UTF-8";
public static void main(String[] args) throws Exception {
String data = "123456";
/**
* AES
*/
String key = "$i%2i9343kl3#df5"; // AES 16位的密钥
String aesEncoded = AesUtils.AESEncrypt(key, data);
String aesDecoded = AesUtils.AESDecrypt(key, aesEncoded);
System.out.println("\naesEncoded: " + aesEncoded + "\naesDecoded: " + aesDecoded);
/**
* RSA
*/
Map<String, String> keys = RSAUtils.generateKey("appKey");
String publicKey = keys.get("publicKey");
String privateKey = keys.get("privateKey");
String rsaEncoded = RSAUtils.publicEncrypt(publicKey, data);
String rsaDecoded = RSAUtils.privateDecrypt(privateKey, rsaEncoded);
System.out.println("\nrsaEncoded: " + rsaEncoded + "\nrsaDecoded: " + rsaDecoded);
String rsaEncodedBySegment = RSAUtils.publicEncryptBySegment(publicKey, data);
String rsaDecodedBySegment = RSAUtils.privateDecryptBySegment(privateKey, rsaEncodedBySegment);
System.out.println("\nrsaEncodedBySegment: " + rsaEncodedBySegment + "\nrsaDecodedBySegment: " + rsaDecodedBySegment);
/**
* Hex
*/
String hexEncoded = HexUtils.byte2hex(data.getBytes(UTF8));
String hexDecoded = new String(HexUtils.hex2byte(hexEncoded), UTF8);
System.out.println("\nhexEncoded: " + hexEncoded + "\nhexDecoded: " + hexDecoded);
/**
*
*/
String signature = SignUtils.sign(data, privateKey);
boolean result = SignUtils.verify(data, signature, publicKey);
System.out.println("\nsignature: " + signature + "\nresult: " + result);
}
}
/**
* AES
*/
class AesUtils {
private static Logger log = LoggerFactory.getLogger(AesUtils.class);
private static final String AESTYPE = "AES/ECB/PKCS5Padding";
public static String AESEncrypt(String keyStr, String plainText) {
byte[] encrypt = null;
try {
SecretKeySpec keySpec = new SecretKeySpec(keyStr.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(AESTYPE);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
encrypt = cipher.doFinal(plainText.getBytes());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return new String(Base64.encodeToString(encrypt, false));
}
public static String AESDecrypt(String keyStr, String encryptData) {
byte[] decrypt = null;
try {
SecretKeySpec key = new SecretKeySpec(keyStr.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(AESTYPE);
cipher.init(Cipher.DECRYPT_MODE, key);
decrypt = cipher.doFinal(Base64.decode(encryptData));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return new String(decrypt);
}
}
/**
* RSA117128
* 使使
*/
class RSAUtils {
// RSA最大加密明文大小
private static final int MAX_ENCRYPT_BLOCK = 117;
// RSA最大解密密文大小分段时不是最大会报错 Caused by: javax.crypto.IllegalBlockSizeException:
// Data must not be longer than 117 bytes
private static final int MAX_DECRYPT_BLOCK = 128;
private static final String UTF8 = "UTF-8";
private static final String RSA_ALGORITHM = "RSA";
private static Logger log = LoggerFactory.getLogger(AesUtils.class);
public static String publicEncrypt(String strPublicKey, String content) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
byte[] keyBytes = base64Decode(strPublicKey);
byte[] encryptData = publicEncrypt(getPublicKey(keyBytes), content.getBytes(UTF8));
return base64Encode(encryptData);
}
public static byte[] publicEncrypt(PublicKey publicKey, byte[] content) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
if (publicKey == null) {
throw new IllegalArgumentException("加密公钥为空");
}
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
public static String privateDecrypt(String strPrivateKey, String encryptData) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
byte[] keyBytes = base64Decode(strPrivateKey);
byte[] content = privateDecrypt(getPrivateKey(keyBytes), base64Decode(encryptData));
return new String(content, UTF8);
}
public static byte[] privateDecrypt(PrivateKey privateKey, byte[] encryptData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
if (privateKey == null) {
throw new IllegalArgumentException("解密私钥为空");
}
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptData);
}
// 生成appkey对应的公钥私钥
public static Map generateKey(String appKey) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();
String publicKey = base64Encode(keyPair.getPublic().getEncoded());
String privateKey = base64Encode(keyPair.getPrivate().getEncoded());
Map map = new HashMap();
map.put("publicKey", publicKey);
map.put("privateKey", privateKey);
System.out.println(map);
return map;
} catch (Exception e) {
log.error(appKey + "生成加密异常!");
}
return null;
}
public static PublicKey getPublicKey(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
return keyFactory.generatePublic(keySpec);
}
public static PrivateKey getPrivateKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
return keyFactory.generatePrivate(keySpec);
}
private static String base64Encode(byte[] data) {
if (data == null)
return "";
return Base64.encodeToString(data, false);
}
private static byte[] base64Decode(String base64String) throws IOException {
if (base64String == null)
return new byte[] {};
return Base64.decode(base64String);
}
// 公钥加密,加密参数过长,采用分段方式
public static String publicEncryptBySegment(String strPublicKey, String content) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException {
byte[] keyBytes = base64Decode(strPublicKey);
PublicKey publicKey = getPublicKey(keyBytes);
if (publicKey == null) {
throw new IllegalArgumentException("加密公钥为空");
}
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return base64Encode(doFinalBySegment(cipher, content.getBytes(UTF8), MAX_ENCRYPT_BLOCK));
}
// 私钥解密,解密参数过长,采用分段方式
public static String privateDecryptBySegment(String strPrivateKey, String content) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, IOException, InvalidKeyException {
byte[] keyBytes = base64Decode(strPrivateKey);
PrivateKey privateKey = getPrivateKey(keyBytes);
if (privateKey == null) {
throw new IllegalArgumentException("解密私钥为空");
}
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(doFinalBySegment(cipher, base64Decode(content), MAX_DECRYPT_BLOCK), UTF8);
}
public static byte[] doFinalBySegment(Cipher cipher, byte[] data, int segmentSize) throws BadPaddingException, IllegalBlockSizeException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < data.length; i += segmentSize) {
byte[] result = cipher.doFinal(ArrayUtils.subarray(data, i, i + segmentSize));
out.write(result);
}
return out.toByteArray();
}
}
/**
* Hash128
* 使
* 1.http http:
* (1)appkeyappKey (2)appSecret
* (3)timeUnix10 2. sign = md5() :
* appKey=test_id&appSecret=A272ADAA3637&time=1423212323&username=lily
* 3.
* appKey=test_id&appSecret=A272ADAA3637&time=1423212323&username=lily&sign=
*/
class SignUtils {
private static final String MD5_ALGORITHM = "MD5";
private static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";
private static final String UTF8 = "UTF-8";
public static String sign(String data, String privateKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
byte[] content = data.getBytes(UTF8);
// 先做摘要
MessageDigest md = MessageDigest.getInstance(MD5_ALGORITHM);
md.update(content);
byte[] digest = md.digest();
// 进行签名
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(RSAUtils.getPrivateKey(Base64.decode(privateKey.getBytes())));
signature.update(digest);
byte[] signByte = signature.sign();
String signText = Base64.encodeToString(signByte, false);
System.out.println(signText);
return signText;
}
public static boolean verify(String data, String signText, String publicKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
byte[] content = data.getBytes(UTF8);
// 先做摘要
MessageDigest md = MessageDigest.getInstance(MD5_ALGORITHM);
md.update(content);
byte[] digest = md.digest();
// 进行验签
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(RSAUtils.getPublicKey(Base64.decode(publicKey.getBytes())));
signature.update(digest);
boolean result = signature.verify(Base64.decode(signText));
return result;
}
}
/**
* Hexhexacsiiurl16
*/
class HexUtils {
private static final int INT_VALUE_2 = 2;
private static char[] c = "0123456789ABCDEF".toCharArray();
public static String byte2hex(byte[] b) {
if (b == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < b.length; i++) {
int v = b[i] & 0xff;
sb.append(c[v / 0x10]).append(c[v % 0x10]);
}
return sb.toString();
}
public static byte[] hex2byte(String s) {
if (s == null) {
return null;
}
byte[] b = s.getBytes();
if ((b.length % INT_VALUE_2) != 0) {
throw new IllegalArgumentException();
}
byte[] b2 = new byte[b.length / INT_VALUE_2];
for (int n = 0; n < b.length; n += INT_VALUE_2) {
String item = new String(b, n, INT_VALUE_2);
b2[n / INT_VALUE_2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
}

View File

@ -1,21 +1,15 @@
package com.rekoe.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.repo.Base64;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
/**
*
*/
public class EncryptUtil {
private final static Log log = Logs.get();
/**
*
*/
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789#@$";
/**
@ -26,8 +20,7 @@ public class EncryptUtil {
/**
*
*
* @param str
*
* @param str
* @return
*/
public static String encrypt(String str) {
@ -63,16 +56,10 @@ public class EncryptUtil {
return ret.toString();
}
public static void main(String[] args) {
String str = "abc";
System.out.println(encrypt(str));
System.out.println(decrypt(encrypt(str)));
}
/**
*
*
* @param str
*
* @param str
* @return
*/
public static String decrypt(String str) {
@ -111,17 +98,11 @@ public class EncryptUtil {
/**
* apache SHA1
*
* @param str
*
* @param str
* @return
*/
public static String encriptSHA1(String str) {
try {
return Base64.encodeToString(MessageDigest.getInstance("SHA1").digest(str.getBytes()), false);
} catch (NoSuchAlgorithmException e) {
log.error(e);
throw new RuntimeException(e);
}
return Base64.encodeToString(DigestUtil.digester(DigestAlgorithm.SHA1).digest(str), false);
}
}