[client:client][client:internal] respect go format

This commit is contained in:
xdrm-brackets 2018-04-24 18:32:00 +02:00
parent ea7d7bb89e
commit e822440581
2 changed files with 49 additions and 73 deletions

View File

@ -39,15 +39,11 @@ func New(ctx *context.T, saveDir string) (*T, error) {
/* (2) Get file management for KEY */
inst.fkey, err = Config(saveDir, "key")
if err != nil {
return nil, err
}
if err != nil { return nil, err }
/* (3) Get file management for SYNC */
inst.fsync, err = Config(saveDir, "sync")
if err != nil {
return nil, err
}
if err != nil { return nil, err }
/* (4) Restore from config */
inst.updateConfig()
@ -101,6 +97,7 @@ func (c *T) Receive(r io.Reader) error {
if uint16(read) != 1 { return errors.New("Cannot read error code") }
fmt.Printf("ERROR CODE : %d\n", errCode[0]);
/* (2) Manage success
---------------------------------------------------------*/
if errCode[0] == 0 {

View File

@ -27,9 +27,7 @@ func (c *T) updateConfig() {
err = c.fkey.Fetch(c.key)
/* (3) On error -> set key to NIL */
if err != nil {
c.key = nil
}
if err != nil { c.key = nil }
/* (4) Create default sync */
c.sync, err = keyset.Create(c.ctx)
@ -38,9 +36,7 @@ func (c *T) updateConfig() {
err = c.fsync.Fetch(c.sync)
/* (6) On error -> set sync to NIL */
if err != nil {
c.sync = nil
}
if err != nil { c.sync = nil }
/* (7) Exit if all keysets have been fetched */
if c.key != nil && c.sync != nil {
@ -49,6 +45,7 @@ func (c *T) updateConfig() {
}
/* (2) If cannot fetch -> create new keysets
---------------------------------------------------------*/
if c.key == nil {
@ -59,19 +56,16 @@ func (c *T) updateConfig() {
c.sync, _ = keyset.Create(c.ctx)
}
/* (3) Store current value
---------------------------------------------------------*/
/* (1) Store key */
err = c.fkey.Store(c.key)
if err != nil {
panic("Cannot store key")
}
if err != nil { panic("Cannot store key") }
/* (2) Store sync */
err = c.fsync.Store(c.sync)
if err != nil {
panic("Cannot store sync")
}
if err != nil { panic("Cannot store sync") }
}
@ -87,9 +81,7 @@ func (c *T) migrateKey() {
/* (2) Regenerate sync */
c.sync, err = keyset.Create(c.ctx)
if err != nil {
panic(err)
}
if err != nil { panic(err) }
/* (3) Store keysets to files */
c.updateConfig()
@ -103,9 +95,7 @@ func (c *T) generateKeyWithConstraints() {
/* Get current hash */
keyHash, err := c.key.CurrentHash()
if err != nil {
panic(err)
}
if err != nil { panic(err) }
/* Search key one is respects contraints */
for true {
@ -113,15 +103,14 @@ func (c *T) generateKeyWithConstraints() {
/* (1) Get current time id
---------------------------------------------------------*/
/* (1) Get time id */
timeId, timeMod := timeid.Generate(c.ctx.Window())
timeID, timeMod := timeid.Generate(c.ctx.Window())
/* (2) Convert timeId to byte array */
timeIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIdBytes, timeId)
timeIDBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIDBytes, timeID)
/* (3) Hash time id */
hashedTimeId, err := scha.Hash(timeIdBytes, 1)
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
if err != nil { continue }
@ -129,6 +118,7 @@ func (c *T) generateKeyWithConstraints() {
---------------------------------------------------------*/
newKey, _ := keyset.Create(c.ctx)
/* (3) Check constraints
---------------------------------------------------------*/
/* (1) Get next hash */
@ -137,11 +127,11 @@ func (c *T) generateKeyWithConstraints() {
/* (2) Get x1 */
x1 := make([]byte, scha.HSIZE)
copy(x1, xor.ByteArray(keyHash, hashedTimeId))
copy(x1, xor.ByteArray(keyHash, hashedTimeID))
/* (3) Get x2 */
x2 := make([]byte, scha.HSIZE)
copy(x2, xor.ByteArray(syncHash, hashedTimeId))
copy(x2, xor.ByteArray(syncHash, hashedTimeID))
/* (4) Get x1 xor x2 */
x := make([]byte, scha.HSIZE)
@ -155,26 +145,18 @@ func (c *T) generateKeyWithConstraints() {
/* (2) Get time mod difference (first byte) */
timeConstraintValue := x[0]%2 == byte(timeMod)
if DEBUG {
fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 2 = %d == %d ? %t\n", x1[0], x2[0], x[0], x[0], x[0]%2, timeMod, timeConstraintValue)
}
if DEBUG { fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 2 = %d == %d ? %t\n", x1[0], x2[0], x[0], x[0], x[0]%2, timeMod, timeConstraintValue) }
/* (4) Retry if invalid time constraint */
if !timeConstraintValue {
continue
}
if !timeConstraintValue { continue }
/* (5) Get migration mod difference (second byte) */
migrationConstraintValue := x[1]%3 == byte(c.key.MigrationCode())
if DEBUG {
fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 3 = %d == %d ? %t\n", x1[1], x2[1], x[1], x[1], x[1]%3, c.key.MigrationCode(), migrationConstraintValue)
}
if DEBUG { fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 3 = %d == %d ? %t\n", x1[1], x2[1], x[1], x[1], x[1]%3, c.key.MigrationCode(), migrationConstraintValue) }
/* (6) Retry if invalid time constraint */
if !migrationConstraintValue {
continue
}
if !migrationConstraintValue { continue }
/* (7) Store new sync */
c.sync = newKey
@ -215,7 +197,6 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
fmt.Printf("Migration code is %d\n", c.key.MigrationCode())
}
/* (3) New sync hash if key consumed
---------------------------------------------------------*/
if c.key.MigrationCode() > 0 {
@ -234,12 +215,11 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
---------------------------------------------------------*/
/* (1) Store current hash */
h0, err := c.key.CurrentHash()
if err != nil {
return err
}
if err != nil { return err }
/* (2) Copy into next hash (same value) */
h1 := make([]byte, scha.HSIZE)
// 1. If migration code = 0 -> use same hash
copy(h1, h0)
@ -249,37 +229,35 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
if err != nil { return err }
}
/* (5) Manage time id
---------------------------------------------------------*/
/* (1) Get current time id */
timeId, timeMod := timeid.Generate(c.ctx.Window())
timeID, timeMod := timeid.Generate(c.ctx.Window())
/* (2) Convert time id to byte array */
timeIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIdBytes, timeId)
timeIDBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIDBytes, timeID)
/* (2) Get digest of time id */
hashedTimeId, err := scha.Hash(timeIdBytes, 1)
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
/* (6) Calculate x1 and x2
---------------------------------------------------------*/
/* (1) Calculate x1 = h ^ h(timeId) */
copy(x1, xor.ByteArray(h0, hashedTimeId))
copy(x1, xor.ByteArray(h0, hashedTimeID))
if DEBUG {
fmt.Printf("\n=== x1 ===\n")
fmt.Printf(" hash is h0 = %x\n", h0)
fmt.Printf(" time id is n = %x[%d]\n", timeIdBytes, timeId)
fmt.Printf(" h(t) = %x\n", hashedTimeId)
fmt.Printf(" time id is n = %x[%d]\n", timeIDBytes, timeID)
fmt.Printf(" h(t) = %x\n", hashedTimeID)
fmt.Printf(" x1 is h0+h(t) = %X\n", x1)
fmt.Printf(" check x1+h(t) eq h0 = %x\n", xor.ByteArray(x1, hashedTimeId))
fmt.Printf(" check x1+h(t) eq h0 = %x\n", xor.ByteArray(x1, hashedTimeID))
fmt.Printf(" check x1+h0 eq h(t) = %x\n", xor.ByteArray(x1, h0))
}
/* (2) Calculate x2 = h ^ h(timeId) ^ timeMod */
copy(x2, xor.ByteArray(h1, hashedTimeId))
copy(x2, xor.ByteArray(h1, hashedTimeID))
// only add time mod if code = 0
if c.key.MigrationCode() == 0 {
@ -290,11 +268,14 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
fmt.Printf("\n=== x2 ===\n")
fmt.Printf(" next is h1 = %x\n", h1)
fmt.Printf(" time mod is m = %x[%d]\n", timeMod, timeMod)
fmt.Printf(" h(t) = %x\n", hashedTimeId)
if c.key.MigrationCode() == 0 { fmt.Printf(" x2 is h1+h(t)+m = %X\n", x2)
} else { fmt.Printf(" x2 is h1+h(t) = %X\n", x2) }
if c.key.MigrationCode() == 0 { fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod))
} else { fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod)) }
fmt.Printf(" h(t) = %x\n", hashedTimeID)
if c.key.MigrationCode() == 0 {
fmt.Printf(" x2 is h1+h(t)+m = %X\n", x2)
fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod))
} else {
fmt.Printf(" x2 is h1+h(t) = %X\n", x2)
fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod))
}
fmt.Printf(" check x2+x1 %% 3 eq o = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[1]%3), uint8(xor.ByteArray(x1, x2)[1]%3) == c.key.MigrationCode())
}
@ -315,18 +296,16 @@ func (c *T) rescue(y1 []byte, y2 []byte) error {
timeMod := uint32(xor.ByteArray(y1, y2)[0] % 2)
/* (2) Try to guess time id from timeM */
timeId := timeid.Guess(c.ctx.Window(), timeMod)
timeIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIdBytes, timeId)
timeID := timeid.Guess(c.ctx.Window(), timeMod)
timeIDBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIDBytes, timeID)
/* (3) Hash timeId */
hashedTimeId, err := scha.Hash(timeIdBytes, 1)
if err != nil {
return err
}
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
if err != nil { return err }
/* (4) Get the received hash */
receivedHash := xor.ByteArray(y1, hashedTimeId)
receivedHash := xor.ByteArray(y1, hashedTimeID)
/* (5) Try to rescue the key */
err = c.key.Rescue(receivedHash)