解析动态JSON并将其编组为结构体。

huangapple go评论88阅读模式
英文:

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"],
})

huangapple
  • 本文由 发表于 2021年6月22日 11:17:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/68077095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定