diff --git a/src/git.xdrm.io/schastsp/server/server.internal.go b/src/git.xdrm.io/schastsp/server/server.internal.go index 41dcf66..3242fd0 100644 --- a/src/git.xdrm.io/schastsp/server/server.internal.go +++ b/src/git.xdrm.io/schastsp/server/server.internal.go @@ -1,13 +1,13 @@ package server import ( + "encoding/binary" + "errors" "fmt" + "git.xdrm.io/schastsp/lib/scha" "git.xdrm.io/schastsp/lib/timeid" "git.xdrm.io/schastsp/lib/xor" - "git.xdrm.io/schastsp/lib/scha" - "encoding/binary" "os" - "errors" ) /* (1) Store hash into file @@ -22,7 +22,7 @@ func (s T) store() error { if err != nil { return err } /* (2) Defer close */ - defer file.Close(); + defer file.Close() /* (3) Write hash into file */ err = binary.Write(file, binary.BigEndian, s.hash) @@ -32,10 +32,6 @@ func (s T) store() error { } - - - - /* (2) Fetch hash from file * * @return err Error or NIL if not @@ -48,7 +44,7 @@ func (s *T) fetch() error { if err != nil { return err } /* (2) Defer close */ - defer file.Close(); + defer file.Close() /* (3) Try to fetch hash from file */ fetchedHash := make([]byte, scha.HSIZE) @@ -67,8 +63,6 @@ func (s *T) fetch() error { } - - /* (3) Request management * * @x1<[]byte> First request component @@ -91,8 +85,8 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { x := xor.ByteArray(x1, x2) /* (2) Extract migration code */ - mcode := uint8(x[1]) % 3; - if DEBUG { fmt.Printf(" extracted code is o = %d\n", mcode) } + mcode := uint8(x[1]) % 3 + if DEBUG { fmt.Printf(" extracted code is o = %d\n", mcode) } /* (3) Fail if no migration but different hashes */ if mcode == 0 && string(x1[2:]) != string(x2[2:]) { @@ -104,27 +98,27 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { ---------------------------------------------------------*/ /* (1) Extract time mod */ timeMod := uint32(x[0]) % 2 - if DEBUG { fmt.Printf(" extracted time mod m = %d\n", timeMod) } + if DEBUG { fmt.Printf(" extracted time mod m = %d\n", timeMod) } /* (2) Try to guess time id */ - timeId := timeid.Guess(s.ctx.Window(), timeMod) - timeIdBytes := make([]byte, 4) - binary.BigEndian.PutUint32(timeIdBytes, timeId) + timeID := timeid.Guess(s.ctx.Window(), timeMod) + timeIDBytes := make([]byte, 4) + binary.BigEndian.PutUint32(timeIDBytes, timeID) /* (3) Hash guessed time id */ - hashedTimeId, err := scha.Hash(timeIdBytes, 1); + hashedTimeID, err := scha.Hash(timeIDBytes, 1) if err != nil { return 2, err } /* (3) Extract hashes ---------------------------------------------------------*/ /* (1) Extract hash from x0 */ - h0 := xor.ByteArray(x1, hashedTimeId) - if DEBUG { fmt.Printf(" supposing hash is h0 = %x\n", h0) } + h0 := xor.ByteArray(x1, hashedTimeID) + if DEBUG { fmt.Printf(" supposing hash is h0 = %x\n", h0) } /* (2) Extract next hash from x1 */ - h1 := xor.ByteArray(x2, hashedTimeId) - if DEBUG { fmt.Printf(" supposing next is h1 = %x\n", h1) } + h1 := xor.ByteArray(x2, hashedTimeID) + if DEBUG { fmt.Printf(" supposing next is h1 = %x\n", h1) } /* (3) Only remove timeMod if migration code = 0 */ if mcode == 0 { @@ -132,7 +126,6 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { } - /* (4) Hash h0 to compare with stored hash 's.hash' ---------------------------------------------------------*/ /* (1) Hash 1 time to check if matches */ @@ -143,8 +136,13 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { if mcode == 2 { hashedH0, err = scha.Hash(h0, s.ctx.MinDepth()) if err != nil { return 2, err } - if DEBUG { fmt.Printf(" hashed is h(min) = %x\n", hashedH0) } - } else if DEBUG { fmt.Printf(" hashed is h(h0) = %x\n", hashedH0) } + + if DEBUG { + fmt.Printf(" hashed is h(min) = %x\n", hashedH0) + } + } else if DEBUG { + fmt.Printf(" hashed is h(h0) = %x\n", hashedH0) + } /* (3) Fail if does not match */ if string(hashedH0) != string(s.hash) { @@ -155,14 +153,12 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { /* (5) Store next hash ---------------------------------------------------------*/ copy(s.hash, h1) - s.store(); + s.store() return 0, nil } - - /* (4) Generate resynchronisation response * * @y1<[]byte> First response component to write into @@ -174,30 +170,29 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { func (s *T) generateResynchronisationResponse(y1 []byte, y2 []byte) error { /* (1) Get current time id */ - timeId, timeMod := timeid.Generate(s.ctx.Window()) + timeID, timeMod := timeid.Generate(s.ctx.Window()) /* (3) Hash the time id */ - timeIdBytes := make([]byte, 4) - binary.BigEndian.PutUint32(timeIdBytes, timeId) + timeIDBytes := make([]byte, 4) + binary.BigEndian.PutUint32(timeIDBytes, timeID) - hashedTimeId, err := scha.Hash(timeIdBytes, 1); + hashedTimeID, err := scha.Hash(timeIDBytes, 1) if err != nil { return err } /* (4) Process y1 = H ^ h(timeId) */ - copy(y1, xor.ByteArray(s.hash, hashedTimeId)) + copy(y1, xor.ByteArray(s.hash, hashedTimeID)) /* (5) Process y2 = H ^ h(timeId) ^ timeMod = y1 ^ timeMod */ - copy(y2, xor.ByteArray(s.hash, hashedTimeId)) + copy(y2, xor.ByteArray(s.hash, hashedTimeID)) y2[0] = xor.Byte(y2[0], byte(timeMod)) if DEBUG { - fmt.Printf("=== y1 ===\n") fmt.Printf(" hash is H = %x\n", s.hash) - fmt.Printf(" time id is t = %x[%d]\n", timeId, timeId) - fmt.Printf(" h(t) = %x\n", hashedTimeId) + fmt.Printf(" time id is t = %x[%d]\n", timeID, timeID) + fmt.Printf(" h(t) = %x\n", hashedTimeID) fmt.Printf(" y1 is H+h(t) = %x\n", y1) - fmt.Printf(" check y1+h(t) eq H = %x\n", xor.ByteArray(y1, hashedTimeId)) + fmt.Printf(" check y1+h(t) eq H = %x\n", xor.ByteArray(y1, hashedTimeID)) fmt.Printf(" check y1+H eq h(t) = %x\n", xor.ByteArray(y1, s.hash)) fmt.Printf("=== y2 ===\n")