func CreateHekaStream(msgBytes []byte, outBytes *[]byte, msc *message.MessageSigningConfig) error { msgSize := uint32(len(msgBytes)) if msgSize > message.MAX_MESSAGE_SIZE { return fmt.Errorf("Message too big, requires %d (MAX_MESSAGE_SIZE = %d)", len(msgBytes), message.MAX_MESSAGE_SIZE) } h := &message.Header{} h.SetMessageLength(msgSize) if msc != nil { h.SetHmacSigner(msc.Name) h.SetHmacKeyVersion(msc.Version) var hm hash.Hash switch msc.Hash { case "sha1": hm = hmac.New(sha1.New, []byte(msc.Key)) h.SetHmacHashFunction(message.Header_SHA1) default: hm = hmac.New(md5.New, []byte(msc.Key)) } hm.Write(msgBytes) h.SetHmac(hm.Sum(nil)) } headerSize := proto.Size(h) if headerSize > message.MAX_HEADER_SIZE { return fmt.Errorf("Message header too big, requires %d (MAX_HEADER_SIZE = %d)", headerSize, message.MAX_HEADER_SIZE) } requiredSize := message.HEADER_FRAMING_SIZE + headerSize + len(msgBytes) if cap(*outBytes) < requiredSize { *outBytes = make([]byte, requiredSize) } else { *outBytes = (*outBytes)[:requiredSize] } (*outBytes)[0] = message.RECORD_SEPARATOR (*outBytes)[1] = uint8(headerSize) // This looks odd but is correct; it effectively "seeks" the initial write // position for the protobuf output to be at the // `(*outBytes)[message.HEADER_DELIMITER_SIZE]` position. pbuf := proto.NewBuffer((*outBytes)[message.HEADER_DELIMITER_SIZE:message.HEADER_DELIMITER_SIZE]) if err := pbuf.Marshal(h); err != nil { return err } (*outBytes)[headerSize+message.HEADER_DELIMITER_SIZE] = message.UNIT_SEPARATOR copy((*outBytes)[message.HEADER_FRAMING_SIZE+headerSize:], msgBytes) return nil }
func CreateHekaStream(msgBytes []byte, outBytes *[]byte) error { h := &message.Header{} h.SetMessageLength(uint32(len(msgBytes))) headerSize := proto.Size(h) requiredSize := message.HEADER_FRAMING_SIZE + headerSize + len(msgBytes) if cap(*outBytes) < requiredSize { *outBytes = make([]byte, requiredSize) } else { *outBytes = (*outBytes)[:requiredSize] } (*outBytes)[0] = message.RECORD_SEPARATOR (*outBytes)[1] = uint8(headerSize) pbuf := proto.NewBuffer((*outBytes)[message.HEADER_DELIMITER_SIZE:message.HEADER_DELIMITER_SIZE]) if err := pbuf.Marshal(h); err != nil { return err } (*outBytes)[headerSize+message.HEADER_DELIMITER_SIZE] = message.UNIT_SEPARATOR copy((*outBytes)[message.HEADER_FRAMING_SIZE+headerSize:], msgBytes) return nil }