reverse bit in byte

697 views
Skip to first unread message

Vasiliy Tolstov

unread,
Aug 16, 2013, 1:54:25 AM8/16/13
to golan...@googlegroups.com
Hello. I'm stuck at simple problem - i ned revers bit from each byte of slice.
Thats i need because:

"""RFB protocol for authentication requires client to encrypt
challenge sent by server with password using DES method. However,
bits in each byte of the password are put in reverse order before
using it as encryption key."""

How can i do that in golang?
Now i'm try something like that, but i get error auth and thinks that
i incorrecting change bits...

for i := 0; i < 8; i++ {
c := pwd[i]
c = ((c & 0x01) << 7) + ((c & 0x02) << 5) + ((c & 0x04) << 3) +
((c & 0x08) << 1) +
((c & 0x10) >> 1) + ((c & 0x20) >> 3) + ((c & 0x40) >> 5) + ((c
& 0x80) >> 7)
newpwd[i] = c
}


--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

Kyle Lemons

unread,
Aug 16, 2013, 2:54:07 AM8/16/13
to Vasiliy Tolstov, golang-nuts
That looks right, but I've usually seen this accomplished by having a lookup table:



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Vasiliy Tolstov

unread,
Aug 16, 2013, 3:07:42 AM8/16/13
to Kyle Lemons, golang-nuts
Thanks!

2013/8/16 Kyle Lemons <kev...@google.com>:

jonathan....@gmail.com

unread,
Aug 16, 2013, 3:15:49 AM8/16/13
to golan...@googlegroups.com
If you're comfortable with assembly, Go is capable of including asssembly functions, although they won't be inlined. Place a file ending in .s in your module folder, and add something like (this if you're targeting x86):

TEXT ·FlipByte(SB),7,$0
    MOVQ addr+0(FP), BP // This gets the address of the byte pointed to. 
    MOVL 0(BP), DX // This stores the byte in DX 
    MOVL $7, CX // Use the CX register as a loop counter 
    XORL AX, AX // Clear the AX register, to store the result in

loop: // This is a label to be jumped to.
     SHLL DX, $1 // Shift the byte in DX right, pushing the leftmost bit into the carry flag 
RCLL AX, $1 // Rotate with carry the value in AX to the left, pushing the bit in the carry flag to the leftmost position in AX. 
     DEC CX // Decrement the counter by one
     JNZ loop // Jump to the loop label if the counter isn't zero

MOVL AX, 0(BP) //Move the result back into the address of the byte pointed to (modify the value pointed to by the pointer passed into the function).
RET //Return from the function

Then declare somewhere in your .go file for that module 

func FlipByte(addr *byte)  

After that, you should be able to call FlipByte like any other function. Note that I haven't tested the above, and I'm not entirely sure about the order of SHLL and RCLL, so if SHLL $1,AX doesn't work, try SHLL AX,$1 . I'm also not sure if Go supports the RCL (rotate with carry function), but if not you can still use it via machine code, like:
BYTE $0x__; BYTE $0x__; BYTE $0x__;
Where the __ values are the opcodes for RCL. 

jonathan....@gmail.com

unread,
Aug 16, 2013, 3:21:20 AM8/16/13
to golan...@googlegroups.com, jonathan....@gmail.com
*That RCLL (rotate with carry left) should be RCRL (rotate with carry right). SHLL shifts the byte in DX left, pushing the leftmost bit in DX into the carry flag, RCRL rotates AX right, pushing the bit from the carry flag into the leftmost bit of AX. Or maybe I'm wrong. As I said, only do this if you're comfortable with an assembly; I just wanted to give you an idea of how easy it is to use assembly in Go.

jonathan....@gmail.com

unread,
Aug 16, 2013, 3:43:19 AM8/16/13
to golan...@googlegroups.com, jonathan....@gmail.com
*And, those instructions ending in L should really use B (as it's a byte, not a long). And the MOVQ should be a MOVL if it's not x86-64 (if it's a 32bit pointer). And DEC may need to be DECB. Go uses non-standard assembly syntax, so it's really a matter of testing it to see what works. 

Skip Tavakkolian

unread,
Aug 16, 2013, 3:43:24 AM8/16/13
to Vasiliy Tolstov, golang-nuts
swap the two nibbles, then each 2bit set, then the bits in each set. for an efficient way of doing it check out "Hacker's Delight" (by Warren). basically:



On Thu, Aug 15, 2013 at 10:54 PM, Vasiliy Tolstov <v.to...@selfip.ru> wrote:

Aram Hăvărneanu

unread,
Aug 16, 2013, 4:36:09 AM8/16/13
to jonathan....@gmail.com, golang-nuts
<jonathan....@gmail.com> wrote:
> Go uses non-standard assembly syntax

There is no standard assembly syntax.

--
Aram Hăvărneanu

jonathan....@gmail.com

unread,
Aug 16, 2013, 4:47:35 AM8/16/13
to golan...@googlegroups.com, jonathan....@gmail.com
You're right. I should have specified that it doesn't follow exactly the syntax of either of the most popular x86 assembly syntaxes, rather it mixes them. It uses the AT&T style source before destination format for MOV instructions for instance, and requires prefixing immediate values with $ like in AT&T style assembly, but doesn't however require prefixing registers with %, so in that last respect is closer to Intel style x86 assembly. It also doesn't seem to allow different kinds of register access; AX is okay, but not RAX or EAX. And it supports a more limited set of mnemonics than other assemblers. 

atkaaz

unread,
Aug 18, 2013, 10:34:54 AM8/18/13
to golang-nuts
Reply all
Reply to author
Forward
0 new messages