给定私钥和消息,不确定如何生成ECDSA签名 [英] Not sure how to generate an ECDSA signature, given a private key and a message

查看:144
本文介绍了给定私钥和消息,不确定如何生成ECDSA签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

I'm following Apple's guide towards composing a CloudKit Web Services request. The bit I'm having trouble with is Step 2, under "Authenticate Web Service Requests":

  1. 使用您的私钥计算此消息的ECDSA签名.

在此之前,我生成了一个.pem文件证书,该文件在文本编辑器中打开时会显示我的私钥,因此我也使用字符串格式.

Before getting to this point, I generated my certificate, a .pem file, which when opening it in a text editor shows me my private key, so I have that in string format too.

我还遵循了生成消息所指内容的步骤,现在将其作为字符串.

I've also followed the steps for generating what it refers to as a message, which I now have as a string.

因此,假设我有一个私钥(或.pem文件(如果需要))和一条消息作为字符串,那么从理论上讲,我应该很容易地获得消息的ECDSA签名,该签名是由我计算得出的私钥.但是,这就是我在努力的地方. 我在网上找到的库似乎采用了更为复杂的方法,具有不同的移动部件,没有参考文献到.pem文件,并讨论生成新的公钥/私钥.

So given that I have a private key, (or the .pem file if required), and a message as a string, it should in theory be fairly simple for me to get a ECDSA signature of the message, computed with my private key. But here's where I'm struggling. Libraries that I've found online seem to take a far more complicated approach, with different moving parts, no reference to a .pem file and talk of generating new public/private keys.

对于此步骤的任何帮助,将不胜感激.

Any help with this step would be greatly appreciated.

推荐答案

看来,目前缺少用于Ruby的OpenSSL EC支持的文档和实际API.特别是,在Ruby< = 2.3.1中,OpenSSL::PKey::EC的签名和验证方式与RSA和DSA密钥使用的API不同.您想要做的,但是目前还不能使用EC键,是这样(这里的所有代码都假定您在某个地方调用了require 'openssl'):

It appears that the documentation and the actual API for Ruby’s OpenSSL EC support are both currently rather lacking. In particular, in Ruby <= 2.3.1 the OpenSSL::PKey::EC doesn’t follow the same API as RSA and DSA keys for signing and verifying. What you would want to do, but currently can’t with EC keys, is this (all the code here assumes you have called require 'openssl' somewhere):

# Get the key, here I'm reading the file
priv_key = OpenSSL::PKey.read(File.read('eckey.pem')) 

# This should be the appropriately formatted string
data = "some data to sign"

# The hash algorithm, I assume SHA256 is being used
digest = OpenSSL::Digest::SHA256.new

# This doesn't work in 2.3.1, but does in 2.4.0-preview1
signature = priv_key.sign(digest, data)

正如我在评论中所指出的那样,这确实适用于Ruby 2.4.0-preview1,但这对您来说可能用处不大.

As I note in the comments, this does work in Ruby 2.4.0-preview1, but that’s likely not much use to you.

要使其与当前的Ruby一起使用,您需要执行以下操作:

To get it working with current Ruby, you need to do something like this:

# As before:
priv_key = OpenSSL::PKey.read(File.read('eckey.pem'))
data = "some data to sign"

signature = priv_key.dsa_sign_asn1(OpenSSL::Digest::SHA256.digest(data))

这两种技术都会为您提供一个二进制字符串.我认为,在将其添加为请求标头之前,您需要对其进行base64编码.

Both these techniques give you a binary string. I think you will need to base64 encode it before adding it as your request header.

提取公共密钥以检查签名是否经过验证也有些棘手(尽管您可以只使用openssl命令行并读入文件). public_key方法返回一个OpenSSL::PKey::EC::Point对象而不是实际密钥,因此我们需要从私钥中重新创建一个. verify方法可以在Ruby 2.3.1上工作:

To extract the public key to check the signature verifies is also a bit tricky (although you could just use the openssl command line and read in the file). The public_key methods returns an OpenSSL::PKey::EC::Point object rather than an actual key, so we need to recreate one from the private key. The verify method does work on Ruby 2.3.1:

pub = OpenSSL::PKey::EC.new(priv_key.group)
pub.public_key = priv_key.public_key

data = "some data to sign"
digest = OpenSSL::Digest::SHA256.new

puts pub.verify(digest, sig, data)

Apple页面上似乎没有指定要使用的哈希算法,但是从我看来,SHA-256是正确的. (另外,我可能完全错了,Apple使用的是完全不同的格式.我很想知道此代码是否对您有用).

The Apple page doesn’t appear to specify the hash algorithm to use, but from what I’ve seen it looks like SHA-256 is right. (Also I could have got this completely wrong and Apple are using a completely different format. I’d be keen to know whether or not this code works you you).

这篇关于给定私钥和消息,不确定如何生成ECDSA签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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