dr5779 2018-02-01 14:35
浏览 29

如何使用Go从专有名称中提取通用名称?

I have an x509 certificate's subject distinguished name (DN) from an X.509 certificate. I want to extract common name (CN) from it. Is there a way to do it via crypto/x509 or any other library in Go?

For Example, if the subject's distinguished name is:

CN=AMA AMI SA APB MDE MADB MDS LE.AXVD-04954-19-17.,OU=Abc,O=DA.CB.AcbDinema.com,dnQualifier=PY0aT8abfcQeUyquTe4w5RVasfY=

then I want to extract common name (CN) part (AMA AMI SA APB MDE MADB MDS LE.AXVD-04954-19-17.) out of it.

  • 写回答

1条回答 默认 最新

  • duanmi3476 2018-02-01 14:54
    关注

    There is nothing in the Go standard library to parse it for you (it only handles ASN.1 encoded distinguished names), but you treat it as just a string and parse it yourself.

    Here is an example using regexps. A word of warning: there is no guarantee that this will work in all cases. For example, I've seen cases of lowercase CN, or ordering may change, or just bad formatting.

    package main
    
    import (
        "fmt"
        "regexp"
        "strings"
    )
    
    func main() {
        subjectString := "CN=AMA AMI SA APB MDE MADB MDS LE.AXVD-04954-19-17.,OU=Abc,O=DA.CB.AcbDinema.com,dnQualifier=PY0aT8abfcQeUyquTe4w5RVasfY="
        re := regexp.MustCompile("CN=([^,]+)")
        matches := re.FindStringSubmatch(subjectString)
    
        fmt.Println(matches[1])
    
        commonNameParts := strings.Split(matches[1], " ")
        fmt.Println(commonNameParts)
    }
    

    Outputs the full CN string and a slice of the individual components of the CommonName:

    AMA AMI SA APB MDE MADB MDS LE.AXVD-04954-19-17.
    [AMA AMI SA APB MDE MADB MDS LE.AXVD-04954-19-17.]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致