duanhanzi8328
2017-07-03 11:47
浏览 224
已采纳

Golang:当输入类型为Reader时,gzip.NewReader()返回nil

I've come across a problem while uncompressing a stream of bytes that are previously compressed. Basically, I'm trying to create a Reader using the function bytes.NewReader() and then to unzip the stream by using gzip.NewReader() function. At last, I'd like to return the real values in either string or byte format.

I know gzip.NewReader requires io.Reader as an input, however, as far as I know, type Reader implements the interface io.Reader. I assume this should not cause any errors but I wonder what might be the problem in this case. I'd really appreciate if you help me out with this!

In case you wonder what this piece of text is,

"amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="

it's a sample input sent from my client python script. It compresses by using gzip, does encryption by using AES128 and finally encodes in base64, in this order.

The client code:

import time
import json
import requests
import random
import gzip
import base64

from Crypto.Cipher import AES

baseurl = 'http://0.0.0.0:80'
key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
iv = 'mb13KcoviZizvYhp'

MODE = AES.MODE_CFB
BLOCK_SIZE = 16
SEGMENT_SIZE = 128

def http_post(url, data):
    print('Going to make a request to {} with the following data: {}'.format(url, data))
    r = requests.post(url,
                  data=data,
                  headers={'Content-type': 'application/json; charset=utf-8',
                           'Connection': 'keep-alive'}, )
    if r.status_code != 200:
        print('Server returned unexpected response code {}, and content: {}'.format(r.status_code, r.content))
        return False
    else:
        data = r.json()
        return data


def post_sample_data(key, iv):
    fake_device_id = "MB88"
    Load_VA_Total_Mean = random.randint(1000, 100000)
    print('Data that should come back: {}'.format(Load_VA_Total_Mean))
    data = {'i': fake_device_id, 'p': [
    {'d': [54.3, 0, 99, 49.35, 3, 99, 51.533, 1, 98, 28964, 7348, 43590, Load_VA_Total_Mean, 10350, 55200, 49.7],
     't': time.time(), 'dt': 'p'}]}
    url = baseurl + '/realtimedata'
    encryption_key_reference = 1
    payload = '{}
{}'.format(convert_pack(data, key, iv), encryption_key_reference)
    return http_post(url, payload)


def convert_pack(inputdict, key, iv):
    jsonpayload = json.dumps(inputdict)  # encode dict to json string
    gzippayload = gzip.compress(jsonpayload.encode('utf-8'))  # compress with gzip
    encryptedpayload = base64.b64encode(encrypt(key, iv, message))
    encoded = base64.b64encode(encryptedpayload)
    print('encoded: {}'.format(encoded))

    return str(encoded, encoding='utf-8')


def _pad_string(value):
    length = len(value)
    pad_size = BLOCK_SIZE - (length % BLOCK_SIZE)
    return value.ljust(length + pad_size, '\x00')


def encrypt(key, iv, plaintext):
    aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE)
    plaintext = _pad_string(plaintext)
    encrypted_text = aes.encrypt(plaintext)
    return encrypted_text


post_sample_data(key, iv)

The server code:

package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"compress/gzip"
"io/ioutil"
"bytes"
)

func main() {
    receivedText := "amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="
    fmt.Println("encrypted + encoded + gzipped: ", originalText)
    key := []byte("TfvY7I358yospfWKcoviZizOShpm5hyH")

    text := decrypt(key, originalText)
    fmt.Println("decrypted: ", string(text))
    reader := bytes.NewReader(text)
    gzReader, err1 := gzip.NewReader(reader)
    fmt.Println(gzReader)

    if err1 != nil {
        fmt.Println("error1")
    }

    content, err2 := ioutil.ReadAll(gzReader)

    if err2 != nil {
        fmt.Println("error2")
    }

    fmt.Println(string(content))
}

func decrypt(key []byte, cryptoText string) []byte {
    ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText)
    fmt.Println("decoded: ", string(ciphertext))
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    iv := []byte("mb13KcoviZizvYhp")
    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(ciphertext, ciphertext)

    return ciphertext
}

The output:

<nil>
error1
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x288 pc=0x10a9167]

goroutine 1 [running]:
io/ioutil.readAll.func1(0xc420037dd0)
    /usr/local/go/src/io/ioutil/ioutil.go:30 +0x119
panic(0x10c4580, 0x1154d00)
    /usr/local/go/src/runtime/panic.go:489 +0x2cf
compress/gzip.(*Reader).Read(0x0, 0xc420092000, 0x200, 0x200, 0x1024ade, 0xc400000008, 0xc4200120c0)
    /usr/local/go/src/compress/gzip/gunzip.go:247 +0x37
bytes.(*Buffer).ReadFrom(0xc420037d28, 0x11451a0, 0x0, 0xc420092000, 0x0, 0x200)
    /usr/local/go/src/bytes/buffer.go:179 +0x160
io/ioutil.readAll(0x11451a0, 0x0, 0x200, 0x0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/io/ioutil/ioutil.go:33 +0x150
io/ioutil.ReadAll(0x11451a0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x20)
    /usr/local/go/src/io/ioutil/ioutil.go:42 +0x3e
main.main()
    /Users/bkaankuguoglu/Desktop/Go-dev/tool-backend/tester.go:43 
+0x404
exit status 2
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题