dsfadsa08911 2012-01-01 14:17
浏览 116
已采纳

Go,DER和处理大整数

I want to create a Google Go application that will be able to DER encode and decode big integers (namely, ECDSA signature r and s). From what I understand I should use the asn1 package, but what functions should I be calling?

For example, I want to encode

316eb3cad8b66fcf1494a6e6f9542c3555addbf337f04b62bf4758483fdc881d

and

bf46d26cef45d998a2cb5d2d0b8342d70973fa7c3c37ae72234696524b2bc812

to get this:

30450220316eb3cad8b66fcf1494a6e6f9542c3555addbf337f04b62bf4758483fdc881d022100bf46d26cef45d998a2cb5d2d0b8342d70973fa7c3c37ae72234696524b2bc81201

and vice versa. Which function should I call for encoding, and which for decoding and how?

  • 写回答

1条回答 默认 最新

  • dongqi19827 2012-01-03 06:50
    关注

    You need to encode an ASN.1 sequence containing both r and s integers as follows:

    ECDSA-Sig-Value ::= SEQUENCE {
      r INTEGER,
      s INTEGER }
    

    Please note that I am not a Go developer but according to the documentation Marshal and Unmarshal functions seems to accept structs in order to encode/decode ASN.1 sequences.

    This code sample seems to work for both encoding and decoding:

    package main
    
    import (
        "fmt"
        "big"
        "asn1"
    )
    
    type ecdsa struct {
        R, S *big.Int
    }
    
    func main() {
        r, _ := new(big.Int).SetString("316eb3cad8b66fcf1494a6e6f9542c3555addbf337f04b62bf4758483fdc881d", 16);
        s, _ := new(big.Int).SetString("bf46d26cef45d998a2cb5d2d0b8342d70973fa7c3c37ae72234696524b2bc812", 16);
        sequence := ecdsa{r, s}
        encoding, _ := asn1.Marshal(sequence)
        fmt.Println(encoding)
        dec := new(ecdsa)
        asn1.Unmarshal(encoding, dec)
        fmt.Println(dec)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置