From: Jeff Halter Date: Wed, 24 Jan 2024 18:41:51 +0000 (-0800) Subject: Rename encode/decode methods X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/76d0c1f61d5981603389e6267bf62636f34bef1f Rename encode/decode methods --- diff --git a/hotline/account.go b/hotline/account.go index c51162d..b041f3c 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -19,7 +19,7 @@ type Account struct { func (a *Account) Read(p []byte) (n int, err error) { fields := []Field{ NewField(FieldUserName, []byte(a.Name)), - NewField(FieldUserLogin, negateString([]byte(a.Login))), + NewField(FieldUserLogin, encodeString([]byte(a.Login))), NewField(FieldUserAccess, a.Access[:]), } diff --git a/hotline/client.go b/hotline/client.go index 3353095..35a7086 100644 --- a/hotline/client.go +++ b/hotline/client.go @@ -580,8 +580,8 @@ func (c *Client) LogIn(login string, password string) error { TranLogin, nil, NewField(FieldUserName, []byte(c.Pref.Username)), NewField(FieldUserIconID, c.Pref.IconBytes()), - NewField(FieldUserLogin, negateString([]byte(login))), - NewField(FieldUserPassword, negateString([]byte(password))), + NewField(FieldUserLogin, encodeString([]byte(login))), + NewField(FieldUserPassword, encodeString([]byte(password))), ), ) } diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index 85f85a4..ee95cc0 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -639,7 +639,7 @@ func HandleSetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error return res, err } - login := DecodeUserString(t.GetField(FieldUserLogin).Data) + login := decodeString(t.GetField(FieldUserLogin).Data) userName := string(t.GetField(FieldUserName).Data) newAccessLvl := t.GetField(FieldUserAccess).Data @@ -710,7 +710,7 @@ func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error res = append(res, cc.NewReply(t, NewField(FieldUserName, []byte(account.Name)), - NewField(FieldUserLogin, negateString(t.GetField(FieldUserLogin).Data)), + NewField(FieldUserLogin, encodeString(t.GetField(FieldUserLogin).Data)), NewField(FieldUserPassword, []byte(account.Password)), NewField(FieldUserAccess, account.Access[:]), )) @@ -755,7 +755,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er } if len(subFields) == 1 { - login := DecodeUserString(getField(FieldData, &subFields).Data) + login := decodeString(getField(FieldData, &subFields).Data) cc.logger.Infow("DeleteUser", "login", login) if !cc.Authorize(accessDeleteUser) { @@ -769,7 +769,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er continue } - login := DecodeUserString(getField(FieldUserLogin, &subFields).Data) + login := decodeString(getField(FieldUserLogin, &subFields).Data) // check if the login dataFile; if so, we know we are updating an existing user if acc, ok := cc.Server.Accounts[login]; ok { @@ -793,8 +793,8 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er } err = cc.Server.UpdateUser( - DecodeUserString(getField(FieldData, &subFields).Data), - DecodeUserString(getField(FieldUserLogin, &subFields).Data), + decodeString(getField(FieldData, &subFields).Data), + decodeString(getField(FieldUserLogin, &subFields).Data), string(getField(FieldUserName, &subFields).Data), acc.Password, acc.Access, @@ -840,7 +840,7 @@ func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error return res, err } - login := DecodeUserString(t.GetField(FieldUserLogin).Data) + login := decodeString(t.GetField(FieldUserLogin).Data) // If the account already dataFile, reply with an error if _, ok := cc.Server.Accounts[login]; ok { @@ -876,7 +876,7 @@ func HandleDeleteUser(cc *ClientConn, t *Transaction) (res []Transaction, err er } // TODO: Handle case where account doesn't exist; e.g. delete race condition - login := DecodeUserString(t.GetField(FieldUserLogin).Data) + login := decodeString(t.GetField(FieldUserLogin).Data) if err := cc.Server.DeleteUser(login); err != nil { return res, err diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index 525bb88..89ee9fc 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -1405,7 +1405,7 @@ func TestHandleGetUser(t *testing.T) { ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ NewField(FieldUserName, []byte("Guest")), - NewField(FieldUserLogin, negateString([]byte("guest"))), + NewField(FieldUserLogin, encodeString([]byte("guest"))), NewField(FieldUserPassword, []byte("password")), NewField(FieldUserAccess, []byte{0, 0, 0, 0, 0, 0, 0, 0}), }, @@ -1533,7 +1533,7 @@ func TestHandleDeleteUser(t *testing.T) { }, t: NewTransaction( TranDeleteUser, &[]byte{0, 1}, - NewField(FieldUserLogin, negateString([]byte("testuser"))), + NewField(FieldUserLogin, encodeString([]byte("testuser"))), ), }, wantRes: []Transaction{ @@ -1564,7 +1564,7 @@ func TestHandleDeleteUser(t *testing.T) { }, t: NewTransaction( TranDeleteUser, &[]byte{0, 1}, - NewField(FieldUserLogin, negateString([]byte("testuser"))), + NewField(FieldUserLogin, encodeString([]byte("testuser"))), ), }, wantRes: []Transaction{ diff --git a/hotline/user.go b/hotline/user.go index 34fe1ab..5b1b705 100644 --- a/hotline/user.go +++ b/hotline/user.go @@ -56,19 +56,19 @@ func ReadUser(b []byte) (*User, error) { return u, nil } -// DecodeUserString decodes an obfuscated user string from a client +// decodeString decodes an obfuscated user string from a client // e.g. 98 8a 9a 8c 8b => "guest" -func DecodeUserString(obfuText []byte) (clearText string) { +func decodeString(obfuText []byte) (clearText string) { for _, char := range obfuText { clearText += string(rune(255 - uint(char))) } return clearText } -// negateString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. +// encodeString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. // The Hotline protocol uses this format for sending passwords over network. // Not secure, but hey, it was the 90s! -func negateString(clearText []byte) []byte { +func encodeString(clearText []byte) []byte { obfuText := make([]byte, len(clearText)) for i := 0; i < len(clearText); i++ { obfuText[i] = 255 - clearText[i] diff --git a/hotline/user_test.go b/hotline/user_test.go index 2268a3e..214b1fb 100644 --- a/hotline/user_test.go +++ b/hotline/user_test.go @@ -77,8 +77,8 @@ func TestDecodeUserString(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotDecodedString := DecodeUserString(tt.args.encodedString); gotDecodedString != tt.wantDecodedString { - t.Errorf("DecodeUserString() = %v, want %v", gotDecodedString, tt.wantDecodedString) + if gotDecodedString := decodeString(tt.args.encodedString); gotDecodedString != tt.wantDecodedString { + t.Errorf("decodeString() = %v, want %v", gotDecodedString, tt.wantDecodedString) } }) } @@ -110,7 +110,7 @@ func TestNegatedUserString(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := negateString(tt.args.encodedString); !bytes.Equal(got, tt.want) { + if got := encodeString(tt.args.encodedString); !bytes.Equal(got, tt.want) { t.Errorf("NegatedUserString() = %x, want %x", got, tt.want) } })