永发信息网

如何验证 oauth 2.0 token是否有效

答案:2  悬赏:50  手机版
解决时间 2021-01-31 18:03
  • 提问者网友:听门外雪花风
  • 2021-01-31 01:07
如何验证 oauth 2.0 token是否有效
最佳答案
  • 五星知识达人网友:渡鹤影
  • 2021-01-31 02:08
这些都是发生在服务器端的交互,如果被窃取了可想你的服务器已经不安全了,不过也没有关系,一个加密的openid对谁来说都没有意义。
全部回答
  • 1楼网友:鸽屿
  • 2021-01-31 03:20
1、hmacsha1的概念 hmacsha1 是 从 sha1 哈希函数构造的一种键控哈希算法,被用作 hmac(基于哈希的消息验证代码)。此 hmac 进程将密钥与消息数据混合,使用哈希函数对混合结果进行哈希计算,将所得哈希值与该密钥混合,然后再次应用哈希函数。输出的哈希值长度为 160 位,可以转换为指定位数。 上面是微软的标准定义,我看了也没太明白,他的作用一句话来理解:就是确认请求的url或者参数是否存在被篡改,以qq 签名为例:发送方(自己)将参数等进行hmac算法计算,将得到的哈希值(即签名值)与请求的参数一同提交至接收方(qq端),然后接收方再次将参数等值 进行hmac算法计算,将得到的哈希值与你传递过来的哈希值进行核对验证,若一样,说明请求正确、验证通过,进行一下步工作,若不一样,将返回错误。 (下面说的够详细了吧,还不理解,留言给我) 2、qq oauth 1.0中用到的哈希算法 /// /// hmacsha1算法加密并返回tobase64string /// /// 签名参数字符串 /// 密钥参数 /// 返回一个签名值(即哈希值) public static string tobase64hmac(string strtext, string strkey) { hmacsha1 myhmacsha1 = new hmacsha1(encoding.utf8.getbytes(strkey)); byte[] bytetext = myhmacsha1.computehash(encoding.utf8.getbytes(strtext)); return system.convert.tobase64string(bytetext); } 或者写成,原理一样: public static string hmacsha1text(string encrypttext, string encryptkey) { //hmacsha1加密 string message; string key; message = encrypttext; key = encryptkey; system.text.asciiencoding encoding = new system.text.asciiencoding(); byte[] keybyte = encoding.getbytes(key); hmacsha1 hmacsha1 = new hmacsha1(keybyte); byte[] messagebytes = encoding.getbytes(message); byte[] hashmessage = hmacsha1.computehash(messagebytes); return bytetostring(hashmessage); } 前面都注释了参数含义,就不再说明了。copy就可使用 注明:页面请引用 using system.security.cryptography; 3、介绍另外一种hmacsha1算法的写法 public static string hmacsha1text(string encrypttext, string encryptkey) { //hmacsha1加密 hmacsha1 hmacsha1 = new hmacsha1(); hmacsha1.key = system.text.encoding.utf8.getbytes(encryptkey); byte[] databuffer = system.text.encoding.utf8.getbytes(encrypttext); byte[] hashbytes = hmacsha1.computehash(databuffer); return convert.tobase64string(hashbytes); }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯