writer.go 682 B

12345678910111213141516171819202122232425262728293031323334
  1. package io
  2. import (
  3. "hash/fnv"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. v2io "github.com/v2ray/v2ray-core/common/io"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. )
  8. type AuthChunkWriter struct {
  9. writer v2io.Writer
  10. }
  11. func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter {
  12. return &AuthChunkWriter{
  13. writer: writer,
  14. }
  15. }
  16. func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
  17. Authenticate(buffer)
  18. return this.writer.Write(buffer)
  19. }
  20. func Authenticate(buffer *alloc.Buffer) {
  21. fnvHash := fnv.New32a()
  22. fnvHash.Write(buffer.Value)
  23. buffer.SliceBack(4)
  24. fnvHash.Sum(buffer.Value[:0])
  25. buffer.Prepend(serial.Uint16Literal(uint16(buffer.Len())).Bytes())
  26. }