使用HMAC或OpenSSL进行URL签名 [英] URL Signing with HMAC or OpenSSL

查看:139
本文介绍了使用HMAC或OpenSSL进行URL签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对网址签名感兴趣(例如 http://. ./?somearg=value&anotherarg=anothervalue&sig=aSKS9F3KL5xc ),但是我有一些要求,使我无解.

I'm interested in url signing (e.g. http://.../?somearg=value&anotherarg=anothervalue&sig=aSKS9F3KL5xc), but I have a few requirements which have left me without a solution yet.

  • 我将在页面上使用PHPPython,所以我需要能够使用两者之一来签名和验证签名.
  • 我的计划是使用priv/pub密钥方案对某些数据进行签名,并能够验证签名是否有效,但这是使签名变得复杂的地方:
  • 验证时不知道数据(不仅是somearg=value&anotherarg=anothervalue)
  • I'll be using either PHP or Python for pages, so I'll need to be able to sign and verify a signature using one of the two.
  • My plan was to use a priv/pub key scheme to sign some data, and be able to verify that the signature is valid, but here's where it gets complicated:
  • The data is not known when the verification is happening (it is not just somearg=value&anotherarg=anothervalue)

我的第一个直觉是使用OpenSSL,例如使用RSA密钥对,可以按照以下方式进行签名:openssl rsautl -sign -inkey private.pem -in sensitive -out privsigned并仅基于privsigned数据和密钥进行验证:openssl rsautl -verify -inkey public.pem -in privsigned -pubin.

My first instinct was to use OpenSSL, e.g. with a RSA keypair, to do something along the lines of signing by: openssl rsautl -sign -inkey private.pem -in sensitive -out privsigned and verifying based on the privsigned data and key ONLY: openssl rsautl -verify -inkey public.pem -in privsigned -pubin.

使用PHP的openssl_get_privatekey()openssl_sign()可以很好地对数据进行签名,但是我需要知道(已解密!)数据以便进行验证(我将没有):openssl_get_publickey()openssl_verify($data, $signature, $pubkeyid); from a href ="http://php.net/openssl_verify" rel ="noreferrer"> http://php.net/openssl_verify .

Using PHP's openssl_get_privatekey() and openssl_sign() signs the data just fine, but I need to know the (decrypted!) data in order to verify (which I will not have): openssl_get_publickey() and openssl_verify($data, $signature, $pubkeyid); from http://php.net/openssl_verify.

还是我在这里想念东西?

Or am I missing something here?

所以我研究了HMAC,但是尽管PythonPHP中都提供了许多哈希函数,但是我对如何进行 verification 哈希. PHPhash_hmac()允许我使用键"(在这种情况下为字符串键)创建哈希.但是我该如何验证哈希是否有效(即&sig=并不仅仅是最终用户&sig=abcdefg1234手动输入的.)

So I looked into HMAC, but although many hash function are available in both Python and PHP, I'm baffled as to how I'd go about verifying the hash. PHP's hash_hmac() allows me to create a hash using a "key" (or in this case a string-key). But how do I go about verifying that a hash is valid (i.e. &sig= hasn't just been manually put in by the end user &sig=abcdefg1234.

因此,总而言之(很长的问题很抱歉):我如何验证服务器的(证书/字符串)密钥已进行签名/哈希处理(假设我无法通过重做所述数据的哈希进行验证) ?您是否对我应该选择哪种路由(Priv/pub-key或HMAC)有任何偏好?

So to sum up (sorry for the long question): How can I verify that a signature/hash has been made by my server's (cert/string)key (given I can not verify by redoing the hash of said data)? And do you have any preferences as to which route I should chose, Priv/pub-key or HMAC?

任何大小的指针都将不胜感激! 预先感谢,

Any pointers big or small is greatly appreciated! Thanks in advance,

  • 乔什

推荐答案

HMAC是对称算法,因此没有单独的创建和检查算法.要进行检查,您只需按照最初应计算的哈希值进行计算,然后检查结果是否等于从客户端实际获得的值.安全性取决于服务器上仅存在的HMAC密钥.

HMAC is a symmetric algorithm, so there is no separate creation and checking algorithm. To check, you simply compute the hash as it should have been computed originally, and check that the result equals what you actually got from the client. The security rests on the HMAC key only existing on your server.

除非您需要签名以不知道的人来验证,否则出于效率方面的考虑,HMAC可能比公钥系统更好.创建或验证公钥签名可能要花费几毫秒的时间(几年前,我将每个实现的时间定为每次操作15毫秒),而HMAC却相当快.

Unless you need the signatures to be verifiable by someone who doesn't know the secret key, HMAC is probably a better choice than public-key systems, for reasons of efficiency. It can take several milliseconds to create or verify a public-key signature (some years ago I timed one implementation at 15 ms per operation), whereas HMAC is quite fast.

(哦,您无法在不知道应该签名的数据的情况下验证任何一种签名.就我所知,这没有任何意义.)

(Oh, and you cannot verify any kind of signature without knowing the data it's supposed to sign. That wouldn't make any sense, as far as I can see).

这篇关于使用HMAC或OpenSSL进行URL签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆