コード例 #1
0
func SampleTest2(c web.C, w http.ResponseWriter, r *http.Request) {

	userId, ok := c.Env[context.UserId]
	if !ok {
		ResError("userId not found", w, err.NewErrWriter())
	}
	fmt.Println("user id   : ", userId)

	secretKey := USER_KEY

	var rec = map[string]interface{}{}
	ew := DecryptAndUnpack(c, rec, secretKey)
	if ew.HasErr() {
		ResError("decrypt and unpack error", w, ew)
		return
	}
	d := analyze(rec)
	fmt.Println("get param : ", d)

	type returnData struct {
		Num int
	}
	ret := new(returnData)

	// 一秒まつ
	time.Sleep(1 * time.Second)

	ret.Num = rand.Intn(100000)
	fmt.Println("ret num   : ", ret.Num)
	ResWrite(c, ret, w)
}
コード例 #2
0
func DecryptAndUnpack(c web.C, out interface{}, secretKey string) err.ErrWriter {
	ew := err.NewErrWriter()

	c.Env[cKey.SecretKey] = secretKey

	// 暗号化データ
	iv := c.Env[cKey.Iv].([]byte)
	cryptData := c.Env[cKey.CryptData].([]byte)

	// decrypt
	ci, err := aes.NewCipher([]byte(secretKey))
	if err != nil {
		return ew.Write(err)
	}
	cbcDecrypter := cipher.NewCBCDecrypter(ci, iv)

	plain := make([]byte, len(cryptData))
	cbcDecrypter.CryptBlocks(plain, cryptData)

	// decode(codec)

	mh := &codec.MsgpackHandle{RawToString: true}
	dec := codec.NewDecoderBytes(plain, mh)
	err = dec.Decode(out)

	if err != nil {
		return ew.Write(err)
	}

	return ew
}
コード例 #3
0
func PackAndEncrypt(c web.C, data interface{}) ([]byte, err.ErrWriter) {
	ew := err.NewErrWriter()

	secretKey := c.Env[cKey.SecretKey].(string)

	// pkcs7 padding function
	pkcs7Pad := func(packed []byte, blockLength int) ([]byte, error) {
		if blockLength <= 0 {
			return nil, fmt.Errorf("invalid block-length %d", blockLength)
		}

		padLen := blockLength - (len(packed) % blockLength)

		pad := bytes.Repeat([]byte{byte(padLen)}, padLen)
		return append(packed, pad...), nil
	}

	var encodeData []byte

	// encode(codec)
	mh := &codec.MsgpackHandle{}
	mh.MapType = reflect.TypeOf(data)
	encoder := codec.NewEncoderBytes(&encodeData, mh)
	e := encoder.Encode(data)
	if e != nil {
		return []byte(""), ew.Write(e)
	}

	// new cipher
	ci, err := aes.NewCipher([]byte(secretKey))
	if err != nil {
		return []byte(""), ew.Write(err)
	}

	// padding
	in, err := pkcs7Pad(encodeData, aes.BlockSize)
	if err != nil {
		return []byte(""), ew.Write(err)
	}

	// encrypt
	iv := make([]byte, len(secretKey))
	out := make([]byte, len(in))
	cbcEncrypter := cipher.NewCBCEncrypter(ci, iv)
	cbcEncrypter.CryptBlocks(out, in)

	encryptData := append(iv, out...)
	return encryptData, ew
}
コード例 #4
0
func BodyAnalyze(c *web.C, r *http.Request, analyzeType AnalyzeType) err.ErrWriter {
	ew := err.NewErrWriter()

	// get body
	bodyBuf := new(bytes.Buffer)
	_, err := bodyBuf.ReadFrom(r.Body)
	if err != nil {
		return ew.Write(err)
	}

	// typeごとに分析
	switch analyzeType {
	case OnUserId:
		bodyAnalyzeUserId(c, bodyBuf.Bytes())
	case OnDefault:
		bodyAnalyzeNewUser(c, bodyBuf.Bytes())
	default:
		ew.Write("undefine analyze type!!")
	}

	return ew
}