@@ -160,6 +160,19 @@ func NewHopPayload(realm byte, hopData *HopData, eob []byte) (HopPayload, error)
160160func (hp * HopPayload ) Realm () byte {
161161 return hp .realm [0 ] & RealmMaskBytes
162162}
163+
164+ // payloadRealm returns the realm that will be used within the raw packed hop
165+ // payload. This differs from the Realm method above in that it uses space to
166+ // encode the packet type and number of frames. The final encoding uses the
167+ // first 4 bits of the realm to encode the number of frames used, and the
168+ // latter 4 bits to encode the real realm type.
169+ func (hp * HopPayload ) payloadRealm () byte {
170+ maskedRealm := hp .realm [0 ] & 0x0F
171+ numFrames := hp .NumFrames ()
172+
173+ return maskedRealm | (byte (numFrames - 1 ) << NumFramesShift )
174+ }
175+
163176// NumFrames returns the total number of frames it'll take to pack the target
164177// HopPayload into a Sphinx packet.
165178func (hp * HopPayload ) NumFrames () int {
@@ -177,34 +190,29 @@ func (hp *HopPayload) NumFrames() int {
177190 return 2 + int (math .Ceil (float64 (remainder )/ 65 ))
178191}
179192
180- // CalculateRealm computes the proper realm encoding in place. The final
181- // encoding uses the first 4 bits of the realm to encode the number of frames
182- // used, and the latter 4 bits to encode the real realm type.
183- func (hp * HopPayload ) CalculateRealm () {
184- maskedRealm := hp .Realm [0 ] & 0x0F
185- numFrames := hp .NumFrames ()
193+ // packRealm writes out the proper realm encoding in place to the target
194+ // io.Writer.
195+ func (hp * HopPayload ) packRealm (w io.Writer ) error {
196+ realm := hp .payloadRealm ()
197+ if _ , err := w .Write ([]byte {realm }); err != nil {
198+ return err
199+ }
186200
187- hp . Realm [ 0 ] = maskedRealm | ( byte ( numFrames - 1 ) << NumFramesShift )
201+ return nil
188202}
189203
190204// Encode encodes the hop payload into the passed writer.
191205func (hp * HopPayload ) Encode (w io.Writer ) error {
192206 // We'll need to add enough padding bytes to position the HMAC at the
193207 // end of the payload
194- padding := hp .NumFrames ()* 65 - len (hp .Payload ) - 1 - 32
208+ padding := hp .NumFrames ()* FrameSize - len (hp .Payload ) - 1 - HMACSize
195209 if padding < 0 {
196210 return fmt .Errorf ("cannot have negative padding: %v" , padding )
197211 }
198212
199- // Before we write the realm out, we need to calculate the current
200- // realm based on the "true" realm as well as the number of frames it
201- // takes to compute the hop payload.
202- hp .CalculateRealm ()
203-
204- if _ , err := w .Write (hp .Realm [:]); err != nil {
213+ if err := hp .packRealm (w ); err != nil {
205214 return err
206215 }
207-
208216 if _ , err := w .Write (hp .Payload ); err != nil {
209217 return err
210218 }
@@ -228,11 +236,11 @@ func (hp *HopPayload) Encode(w io.Writer) error {
228236// Decode unpacks an encoded HopPayload from the passed reader into the target
229237// HopPayload.
230238func (hp * HopPayload ) Decode (r io.Reader ) error {
231- if _ , err := io .ReadFull (r , hp .Realm [:]); err != nil {
239+ if _ , err := io .ReadFull (r , hp .realm [:]); err != nil {
232240 return err
233241 }
234242
235- numFrames := int (hp .Realm [0 ]>> NumFramesShift ) + 1
243+ numFrames := int (hp .realm [0 ]>> NumFramesShift ) + 1
236244 numBytes := (numFrames * FrameSize ) - 32 - 1
237245
238246 hp .Payload = make ([]byte , numBytes )
0 commit comments