使用 Go 生成带有 DSA 签名的 SHA256,然后使用 Java 对其进行验证

  1. 去sha256原始数据,然后DSA签名

  2. 按十六进制打印符号结果

  3. 使用 Java SHA256 和DSA 验证第一步结果是否返回错误

  4. 我知道沙256然后DSA是差异SHA256与DSA

  5. 我怎么能在Go中生成一个符号,可以在Java中使用sha256与dsa进行验证?

  6. Go是否有任何其他第三方库支持?

package main


import (

    "crypto/dsa"

    "crypto/rand"

    "crypto/sha256"

    "encoding/asn1"

    "encoding/hex"

    "fmt"

    "golang.org/x/crypto/ssh"

    "math/big"

)


func main() {

    pemData := []byte("-----BEGIN DSA PRIVATE KEY-----\n" +

        "MIIBvAIBAAKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR\n" +

        "+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb\n" +

        "+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdg\n" +

        "UI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlX\n" +

        "TAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCj\n" +

        "rh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQB\n" +

        "TDv+z0kqAoGBAIb9o0KPsjAdzjK571e1Mx7ZhEyJGrcxHiN2sW8IztEbqrKKiMxp\n" +

        "NlTwm234uBdtzVHE3uDWZpfHPMIRmwBjCYDFRowWWVRdhdFXZlpCyp1gMWqJ11dh\n" +

        "3FI3+O43DevRSyyuLRVCNQ1J3iVgwY5ndRpZU7n6y8DPH4/4EBT7KvnVAhR4Vwun\n" +

        "Fhu/+4AGaVeMEa814I3dqg==\n" +

        "-----END DSA PRIVATE KEY-----")


    p, _ := ssh.ParseRawPrivateKey(pemData)

    pp := p.(*dsa.PrivateKey)


    hashed := []byte{1}

    sha256 := sha256.New()

    sha256.Write(hashed)

    hashed = sha256.Sum(nil)

    r, s, _ := dsa.Sign(rand.Reader, pp, hashed)

    type dsaSignature struct {

        R, S *big.Int

    }

    var ss dsaSignature

    ss.S = s

    ss.R = r

    signatureBytes, _ := asn1.Marshal(ss)

    fmt.Println(hex.EncodeToString(signatureBytes))

}


LEATH
浏览 113回答 2
2回答

慕妹3242003

如果发生这种情况,请遵循 FIPS186-4 第 4.6 节和第 4.7 节hashed := []byte{1}pemData, _ := ioutil.ReadFile("config/secure/private.key")p, err := ssh.ParseRawPrivateKey(pemData)pp, ok := p.(*dsa.PrivateKey)h := sha256.New()h.Write(hashed)hashed := h.Sum(nil)if pp.Q.BitLen()/8 < len(hashed) {&nbsp; &nbsp; hashed = hashed[0 : pp.Q.BitLen()/8]}r, s, _ := dsa.Sign(rand.Reader, pp, hashed)var ss dsaSignaturess.S = sss.R = rhashed, _ = asn1.Marshal(ss)

神不在的星期二

我在爪哇中发现package sun.security.provider;private BigInteger generateS(BigInteger var1, BigInteger var2, BigInteger var3, BigInteger var4) throws SignatureException {&nbsp; &nbsp; &nbsp; &nbsp; byte[] var5;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var5 = this.md.digest();&nbsp; &nbsp; &nbsp; &nbsp; } catch (RuntimeException var9) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new SignatureException(var9.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // this line split the hash256 redsult&nbsp; &nbsp; &nbsp; &nbsp; int var6 = var2.bitLength() / 8;&nbsp; &nbsp; &nbsp; &nbsp; if (var6 < var5.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var5 = Arrays.copyOfRange(var5, 0, var6);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; BigInteger var7 = new BigInteger(1, var5);&nbsp; &nbsp; &nbsp; &nbsp; BigInteger var8 = var4.modInverse(var2);&nbsp; &nbsp; &nbsp; &nbsp; return var1.multiply(var3).add(var7).multiply(var8).mod(var2);}你可以在戈朗使用&nbsp; &nbsp; hashed := []byte{1}&nbsp; &nbsp; sha256 := sha2562.New()&nbsp; &nbsp; sha256.Write(hashed)&nbsp; &nbsp; hashed = sha256.Sum(nil)&nbsp; &nbsp; // you must split it&nbsp;&nbsp; &nbsp; hashed = hashed[0:20]原因是sha256结果在java中分裂,但glang没有分裂
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go