Search in sources :

Example 41 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class SHA1 method getSHA1.

/**
 * <p>
 * 用SHA1算法生成安全签名
 * </p>
 *
 * @param token
 * 				票据
 * @param timestamp
 * 				时间戳
 * @param nonce
 * 				随机字符串
 * @param encrypt
 * 				密文
 * @return 安全签名
 *
 * @throws AESException {@link AESException}
 */
public static String getSHA1(String token, String timestamp, String nonce, String encrypt) {
    try {
        String[] array = new String[] { token, timestamp, nonce, encrypt };
        StringBuffer sb = new StringBuffer();
        /* 字符串排序 */
        Arrays.sort(array);
        for (int i = 0; i < 4; i++) {
            sb.append(array[i]);
        }
        /* SHA1签名生成 */
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(sb.toString().getBytes());
        byte[] digest = md.digest();
        StringBuffer hexstr = new StringBuffer();
        String shaHex = "";
        for (int i = 0; i < digest.length; i++) {
            shaHex = Integer.toHexString(digest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexstr.append(0);
            }
            hexstr.append(shaHex);
        }
        return hexstr.toString();
    } catch (Exception e) {
        throw new JeesuiteBaseException(500, "error", e);
    }
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) MessageDigest(java.security.MessageDigest) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Aggregations

JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)41 IOException (java.io.IOException)14 Request (okhttp3.Request)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 CObjectMetadata (com.jeesuite.cos.CObjectMetadata)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SignatureException (java.security.SignatureException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CUploadResult (com.jeesuite.cos.CUploadResult)2 WrapperResponseEntity (com.jeesuite.springweb.model.WrapperResponseEntity)2 COSObject (com.qcloud.cos.model.COSObject)2 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)2 QiniuException (com.qiniu.common.QiniuException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2