将 *crypto.PrivateKey 转换为 *rsa.PrivateKey

huangapple go评论76阅读模式
英文:

Converting *crypto.PrivateKey to *rsa.PrivateKey

问题

我正在开发一种根据传入的私钥类型生成签名的方法。为了做到这一点,我将一个 *crypto.PrivateKey 变量作为参数传递。但是当我想要进行类型断言以使用该参数时,问题就出现了。

func Sign(text string, privKey *crypto.PrivateKey) string{
if _, ok := (*privKey).(rsa.PrivateKey) //这样是可以的
if _, ok := (privKey).(*rsa.PrivateKey) //这样是不可以的

我猜我可以做第一种方式,因为如果我使用"*",编译器会认为它是一个接口。但是我不知道为什么我不能做第二种方式,当它应该是正确的时候。

顺便说一下,我需要使用一个 *rsa.PrivateKey 类型的变量,我可以这样做:

privRsa, _ := (*privKey).(rsa.PrivateKey)
var priv *rsa.PrivateKey
priv = &privRsa

我无法理解的是为什么我不能直接将 *crypto.PrivateKey 转换为 *rsa.PrivateKey,或者是否存在一种方法可以做到这一点。我认为,按照我目前的做法,这样做会在内存中分配新的空间,如果我希望它高效,就不应该这样做。

英文:

I am developing a method that will make a signature based in the type of private key that is passed as argument. To do that, I'm passing a *crypto.PrivateKey variable as argument. But the problem comes when I want to do a type assertion to use the argument.

func Sign(text string, privKey *crypto.PrivateKey) string{
if _, ok := (*privKey).(rsa.PrivateKey) //This is ok
if _, ok := (privKey).(*rsa.PrivateKey) //This is not ok 2

I guess I can do the first thing because if I use "*", the compiler think that it is an interface. But I don't know why I can't do the second thing, when it should be correct.

By the way, I need to use a variable of type *rsa.PrivateKey, I can do this this way:

privRsa, _ := (*privKey).(rsa.PrivateKey)
var priv *rsa.PrivateKey
priv = &privRsa

What I can not understand is why I can't directly convert the *crypto.PrivateKey into *rsa.PrivateKey, or if it exist a way to do that. I thing doing it the way I'm doing it right now would allocate new space in memory that I shouldn't if I want it to be efficient

答案1

得分: 1

crypto.PrivateKey 是一个接口。*crypto.PrivateKey 是一个指向接口的指针。你可以在接口上使用类型断言来获取底层值:

func Sign(text string, privKey crypto.PrivateKey) string {
   if _, ok := privKey.(rsa.PrivateKey); ok {
      // ...
   }
}

// ...

var pkey rsa.PrivateKey
Sign(text, pkey)

在这里,接口中的私钥值是 rsa.PrivateKey 的一个副本。

如果你传递 pkey 的地址,那么接口将持有私钥的指针:

func Sign(text string, privKey crypto.PrivateKey) string {
   if _, ok := privKey.(*rsa.PrivateKey); ok {
      // ...
   }
}

// ...

var pkey rsa.PrivateKey
Sign(text, &pkey)
英文:

crypto.PrivateKey is an interface. *crypto.PrivateKey is a pointer to an interface. You can use type-assertion on an interface to get the underlying value:

func Sign(text string, privKey crypto.PrivateKey) string{
   if _, ok := privKey.(rsa.PrivateKey) 
...
}

...
var pkey rsa.PrivateKey
Sign(text,pkey)

Here, the private key value in the interface is a copy of the rsa.PrivateKey.

If you pass the address of pkey, then, then the interface would have a pointer to the private key:

func Sign(text string, privKey crypto.PrivateKey) string{
   if _, ok := privKey.(*rsa.PrivateKey) 
...
}

...
var pkey rsa.PrivateKey
Sign(text,&pkey)

huangapple
  • 本文由 发表于 2022年2月22日 23:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/71224040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定