![](/style/images/good.png)
![](/style/images/bad.png)
【android】 加解密算法 hmac 的使用 | iTimeTraveler
source link: https://itimetraveler.github.io/2015/12/31/%E3%80%90Android%E3%80%91%20%E5%8A%A0%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95%20HMAC%20%E7%9A%84%E4%BD%BF%E7%94%A8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【Android】 加解密算法 HMAC 的使用
1、HMAC算法
HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。
简而言之,HMAC就是含有密钥散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加上了密钥。因此MAC算法也经常被称作HMAC算法。关于hmac算法的详情可以参看RFC 2104(http://www.ietf.org/rfc/rfc2104.txt),这里包含了HmacMD5算法的C语言实现。
2、代码实现(Android)
//这是HMAC的Android代码
//之所以不是Java是因为代码中的Base64使用的是android.util包下的Base64类,而不是Java自带的Base64类。
public class HMACTest {
private static final String LOG_TAG = "HMACTest";
private static final String REGISTER_HMAC_KEY = "12a9cc3f-1fd9-48a3-1fd9-1fd9d027ac2";
private String stringToSign(String data) {
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(
REGISTER_HMAC_KEY.getBytes("UTF-8"), mac.getAlgorithm());
mac.init(secret);
return Base64.encodeToString(mac.doFinal(data.getBytes()), Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
Log.e(LOG_TAG, "Hash algorithm SHA-1 is not supported", e);
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Encoding UTF-8 is not supported", e);
} catch (InvalidKeyException e) {
Log.e(LOG_TAG, "Invalid key", e);
}
return "";
}
/*
* 测试函数
*/
public static void test() {
HMACTest hmac = new HMACTest();
String str = "Bello, Miss.Seven";
System.out.println("加密前:" + str);
System.out.println("加密后:" + hmac.stringToSign(str));
}
}
Recommend
-
38
MD4、MD5、SHA1、HMAC、HMAC_SHA1 区别
-
31
hmac包实现了U.S.Federal Infomation Processing Standards Publication 198规定的HMAC(加密哈希信息认证码)。 HMAC是使用key标记信息的加密hash。接收者使用相同的key逆运算来认证hash。 出于安全目的,接收者...
-
7
关于HMAC签名相关 2020-11-04 ...
-
8
简介 Tinker: n. 〈英〉小炉匠,补锅匠,修补匠 Tinker 是微信官方开源的 Android 热修复框架,支持在无需升级APK的前...
-
6
【Android】使用shape的同时,动态替换shape的颜色属性 在实现布局的时候,有些按钮形状相同,只是颜色有差异,如果使用自定义shape实现了其中一种按钮,有没有可能不需要再为其他每个颜色都...
-
7
一、跨进程通信为了演示方便,将Service与Activity处于不同的进程,可以在AndroidManifest.xml中,把service配置成android:process=":remote" ,也可以命名成其他的。 (1) IRemoteService.aidl:定义Server端提供的服务接口...
-
16
VPN抓包使用VPN技术可以直接获得网络三层IP报文,可以不可以基于此实现移动端抓包呢?肯定可以,Android已经有大批开源库基于该思路实现抓包。我们来学习下原理。 VpnServiceVpnService是开发Android VPN的基础,下面是
-
9
简介当程序崩溃(Crash)的时候,默认是不对异常信息做处理的。如果想要把异常信息保存到本地文件中,或上传的服务器。那么就要借助UncaughtExceptionHandler这个类。 使用方法
-
6
ever gauzy v0.281.9 - JWT weak HMAC secret ...
-
1
HMAC Examples in API Connect HMAC Examples in API Connect Apr 15, 2023 • Category
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK