Java實現(xiàn)SHA1加密代碼實例
更新時間:2018年07月17日 08:16:20 作者:花2不謝
這篇文章給大家分享了Java實現(xiàn)SHA1加密的相關(guān)實例代碼,有興趣的朋友可以測試參考下。
微信接入中需要用到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)換成十六進制的字符串形式 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ù)制新建一個類即可使用,我自己做好的,測試微信接入成功。
相關(guān)文章
SpringBoot 指標(biāo)監(jiān)控actuator的專題
未來每一個微服務(wù)在云上部署以后,我們都需要對其進行監(jiān)控、追蹤、審計、控制等。SpringBoot就抽取了Actuator場景,使得我們每個微服務(wù)快速引用即可獲得生產(chǎn)級別的應(yīng)用監(jiān)控、審計等功能,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下2021-11-11SpringDataJpa創(chuàng)建聯(lián)合索引的實現(xiàn)
這篇文章主要介紹了SpringDataJpa創(chuàng)建聯(lián)合索引的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
在Spring Boot中,@Value注解是一個非常有用的特性,它允許我們將外部的配置注入到我們的Bean中,@ConfigurationProperties用于將配置文件中的屬性綁定到 Java Bean 上,本文介紹了@Value獲取值和@ConfigurationProperties獲取值用法及比較,需要的朋友可以參考下2024-08-08spring cloud gateway全局過濾器實現(xiàn)向request header中放數(shù)據(jù)
這篇文章主要介紹了spring cloud gateway全局過濾器實現(xiàn)向request header中放數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07