英文:
Unmarshal dynamic JSON and marshal into struct
问题
我通过API发送了一个由Ruby生成的解密消息
rawData = {value: 1}
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.encrypt
cipher.iv = iv
cipher.key = key
cipher.auth_data = auth_data
cipherText = cipher.update(JSON.generate(rawData)) + cipher.final
authTag = cipher.auth_tag
hexString = (iv + cipherText + authTag).unpack('H*').first
# result
# "366138323563323565613734cb4ebc43e13176adb7825ddc3806e8fa9abaf39aa32ef210568c08"
我想在Golang中解密
type ResultPlainText struct {
Plaintext string `json:"plaintext"`
}
type PermitRequest struct {
Data string `json:"data"`
}
// request body will be
// { "data": "366138323563323565613734cb4ebc43e13176adb7825ddc3806e8fa9abaf39aa32ef210568c08" }
func requestPlainText(w http.ResponseWriter, r *http.Request){
var permit PermitRequest
_ = json.NewDecoder(r.Body).Decode(&permit)
permitData := permit.Data
encodedStr, _ := hex.DecodeString(permit.Data)
key := []byte("972ec8dd995743d981417981ac2f30db")
authData := []byte("73f6828fc5be")
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
aesGcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
sz := aesGcm.NonceSize()
nonce, cipherText := encodedStr[:sz], encodedStr[sz:]
rawData, err := aesGcm.Open(nil, nonce, cipherText, authData)
if err != nil {
panic(err.Error())
}
var buffData map[string]interface{}
f := json.Unmarshal(rawData, &buffData)
if f != nil {
panic(f.Error())
}
plaintext := buffData["value"].(string)
result := ResultPlainText{ Plaintext: plaintext}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(result)
}
我想直接将其放入结构体类型中。
得到这个错误
Unresolved reference 'String'
英文:
I send a decrypted message produced from ruby via API
rawData = {value: 1}
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.encrypt
cipher.iv = iv
cipher.key = key
cipher.auth_data = auth_data
cipherText = cipher.update(JSON.generate(rawData)) + cipher.final
authTag = cipher.auth_tag
hexString = (iv + cipherText + authTag).unpack('H*').first
# result
# "366138323563323565613734cb4ebc43e13176adb7825ddc3806e8fa9abaf39aa32ef210568c08"
and want decrypt in golang
type ResultPlainText struct {
Plaintext string `json:"plaintext"`
}
type PermitRequest struct {
Data string `json:"data"`
}
// request body will be
// { "data": "366138323563323565613734cb4ebc43e13176adb7825ddc3806e8fa9abaf39aa32ef210568c08" }
func requestPlainText(w http.ResponseWriter, r *http.Request){
var permit PermitRequest
_ = json.NewDecoder(r.Body).Decode(&permit)
permitData := permit.Data
encodedStr, _ := hex.DecodeString(permit.Data)
key := []byte("972ec8dd995743d981417981ac2f30db")
authData := []byte("73f6828fc5be")
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
aesGcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
sz := aesGcm.NonceSize()
nonce, cipherText := encodedStr[:sz], encodedStr[sz:]
rawData, err := aesGcm.Open(nil, nonce, cipherText, authData)
if err != nil {
panic(err.Error())
}
var buffData map[string]interface{}
f := json.Unmarshal(rawData, &buffData)
if f != nil {
panic(f.Error())
}
plaintext := buffData["value"].String()
result := ResultPlainText{ Plaintext: plaintext}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(result)
}
I want to put directly into the struct type..
Get this error
Unresolved reference 'String'
答案1
得分: 1
你可以使用json.RawMessage
作为明文类型,而不是使用string
。
type ResultPlainText struct {
Plaintext json.RawMessage `json:"plaintext"`
}
// ...
var buffData map[string]json.RawMessage
f := json.Unmarshal(rawData, &buffData)
if f != nil {
panic(f.Error())
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(ResultPlainText{
Plaintext: buffData["value"],
})
英文:
You can use json.RawMessage
instead of string
as the plaintext type.
type ResultPlainText struct {
Plaintext json.RawMessage `json:"plaintext"`
}
// ...
var buffData map[string]json.RawMessage
f := json.Unmarshal(rawData, &buffData)
if f != nil {
panic(f.Error())
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(ResultPlainText{
Plaintext: buffData["value"],
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论