[server:internal] respect go format

This commit is contained in:
xdrm-brackets 2018-04-24 18:26:54 +02:00
parent 88055b944e
commit ea7d7bb89e
1 changed files with 33 additions and 38 deletions

View File

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