声明类型 *big.Int 溢出常量 golang
Declaring type *big.Int overflowing constant golang
使用 crypto/rsa 包生成密钥对很简单,但自己声明它很痛苦。
我正在尝试声明类型为 rsa.PublicKey 的变量,其定义为:
type PublicKey struct {
N *big.Int // modulus
E int // public exponent
}
我已经尝试了一百零三种方法,但我的代码目前看起来像:
PublicKey := new(rsa.PublicKey)
PublicKey.N = 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799
PublicKey.E = 65537
我收到这个错误:
./main.go:21: constant too large: 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799
./main.go:21: overflow in constant
./main.go:21: cannot use 0 (type int) as type *big.Int in assignment
有什么帮助吗?
问题是 Go 将您的文字值解释为 int。但是,它并不真正适合 32 位,所以它失败了。但是,您可以在创建后设置 big.Int
的值:
PublicKey.N = big.NewInt(0)
PublicKey.N.SetBytes([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
fmt.Println(N)
// 8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529
T. Claverie 的回答将创建一个 public 密钥,但不会将模数 (PublicKey.N) 设置为您看到的数字。相反,它将使用这些数字的字符代码。
fmt.Print([]byte("1"))
[49]
您可以使用 big.Int.UnmarshalText() 获取您在输入字符串中看到的密钥。
PublicKey := new(rsa.PublicKey)
PublicKey.N = big.NewInt(0)
PublicKey.N.UnmarshalText([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
你可以在这里看到区别:https://play.golang.org/p/HL6YNdcSubv
使用 crypto/rsa 包生成密钥对很简单,但自己声明它很痛苦。
我正在尝试声明类型为 rsa.PublicKey 的变量,其定义为:
type PublicKey struct {
N *big.Int // modulus
E int // public exponent
}
我已经尝试了一百零三种方法,但我的代码目前看起来像:
PublicKey := new(rsa.PublicKey)
PublicKey.N = 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799
PublicKey.E = 65537
我收到这个错误:
./main.go:21: constant too large: 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799
./main.go:21: overflow in constant
./main.go:21: cannot use 0 (type int) as type *big.Int in assignment
有什么帮助吗?
问题是 Go 将您的文字值解释为 int。但是,它并不真正适合 32 位,所以它失败了。但是,您可以在创建后设置 big.Int
的值:
PublicKey.N = big.NewInt(0)
PublicKey.N.SetBytes([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
fmt.Println(N)
// 8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529
T. Claverie 的回答将创建一个 public 密钥,但不会将模数 (PublicKey.N) 设置为您看到的数字。相反,它将使用这些数字的字符代码。
fmt.Print([]byte("1"))
[49]
您可以使用 big.Int.UnmarshalText() 获取您在输入字符串中看到的密钥。
PublicKey := new(rsa.PublicKey)
PublicKey.N = big.NewInt(0)
PublicKey.N.UnmarshalText([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
你可以在这里看到区别:https://play.golang.org/p/HL6YNdcSubv