国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Java實(shí)現(xiàn)SHA1加密代碼實(shí)例

2024-07-14 08:41:37
字體:
供稿:網(wǎng)友

微信接入中需要用到SHA1的算法。Java版的SHA1加密如下:

/* * 微信公眾平臺(JAVA) SDK * * Copyright (c) 2016, Ansitech Network Technology Co.,Ltd All rights reserved. * http://www.ansitech.com/weixin/sdk/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.levi.utils; import java.security.MessageDigest; /** * <p>Title: SHA1算法</p> * * @author levi */public final class SHA1 {   private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',              '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};   /**   * Takes the raw bytes from the digest and formats them correct.   *   * @param bytes the raw bytes from the digest.   * @return the formatted bytes.   */  private static String getFormattedText(byte[] bytes) {    int len = bytes.length;    StringBuilder buf = new StringBuilder(len * 2);    // 把密文轉(zhuǎn)換成十六進(jìn)制的字符串形式    for (int j = 0; j < len; j++) {      buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);      buf.append(HEX_DIGITS[bytes[j] & 0x0f]);    }    return buf.toString();  }   public static String encode(String str) {    if (str == null) {      return null;    }    try {      MessageDigest messageDigest = MessageDigest.getInstance("SHA1");      messageDigest.update(str.getBytes());      return getFormattedText(messageDigest.digest());    } catch (Exception e) {      throw new RuntimeException(e);    }  }}

如果需要做微信接入,直接把上面的復(fù)制新建一個(gè)類即可使用,我自己做好的,測試微信接入成功。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 琼中| 赤水市| 丰城市| 鄯善县| 洛隆县| 遂川县| 齐河县| 大连市| 启东市| 瑞丽市| 盐山县| 江阴市| 中方县| 宣恩县| 德令哈市| 东阳市| 伊川县| 临江市| 铁岭市| 芜湖县| 龙州县| 饶平县| 望江县| 旌德县| 教育| 布拖县| 许昌县| 峨边| 桦南县| 郴州市| 德钦县| 清远市| 海南省| 连州市| 株洲县| 海阳市| 车险| 武胜县| 乌审旗| 崇州市| 吴桥县|