Announcement

Collapse
No announcement yet.

An algo for making messy random strings.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • An algo for making messy random strings.

    The algo is to produce random string data to be used with Windows Class names or the strings for private messages. It is primarily to make this data impossible to predict as it changes every time the app is started.

    http://masm32.com/board/index.php?topic=8881.0

    hutch at movsd dot com
    The MASM Forum

    www.masm32.com

  • #2

    Comment


    • #3
      hutch at movsd dot com
      The MASM Forum

      www.masm32.com

      Comment


      • #4
        Another way:

        #Compile exe
        #Dim All
        #Include "NTSecAPI.inc"

        FunctionPBmain() As Long
        Local szClassName as String * 64
        RtlGenRandom( szClassName, 64 )
        Waitkey$
        End Function

        Comment


        • #5
          Originally posted by David Roberts View Post
          Another way:
          There must be dozens of ways at least.
          Creating a random string is about week 2 of "Programming 101". Does it really warrant an entry in the Source Code Library"?

          Comment


          • #6
            Originally posted by Stuart
            Does it really warrant an entry in the Source Code Library"?
            I do not think that mine does which was posted because I felt that Steve's method was a bit of a 'dog's dinner'. He seems to be flogging the death out of ! rdtsc, ! bswap eax.

            Oops, this is not the place for this conversation.

            Comment


            • #7
              So he's still using the totally pointless bswap ?

              Comment


              • #8
                The level of literacy here is truly mind boggling.

                I flog to death MOV ADD SUB CMP JMP Jxx MOVSB MOVSD MOVSQ and a whole host of other Intel mnemonics, what is the bias against RDTSC BSWAP and MOV ?

                It does what it does and while it appears that Stuart can live with sequential number sequences that are easily predicted, many of us can't when we need very hard (read impossible) to predict seed numbers.

                > RtlGenRandom( szClassName, 64 )

                I imagine it would work but why call a random number generator as a seeding source when you can do it with two instructions ? Surely code economy (read small and fast) has some mileage here.

                You guys must be bored, now here is a suggestion, take up assembler programming, the easy stuff in 32 bit or the PHUN stuff in 64 bit.

                Brought to you from my magnificent 12 core Xeon which I have just about finished, hardware is up and going, just added another 4tb disk and all of the software is close to configured.
                hutch at movsd dot com
                The MASM Forum

                www.masm32.com

                Comment


                • #9
                  Just so that you don't feel that there is enough variation in the simple reseed technique, here is a slightly more complex version of a reseeding algo in 64 bit.
                  Code:
                  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  
                   NOSTACKFRAME
                  
                   reseed proc
                  
                      mov rax, 100                                ; out of range number
                      cpuid                                       ; set all regs to 0
                      rdtsc                                       ; date time counter
                      pause                                       ; spinlock pause
                      bswap rax                                   ; reverse byte order
                      mov r11, rax                                ; store rax in r11
                  
                      mov rcx, 10                                 ; loop count
                    @@:
                      rdtsc                                       ; date time counter
                      pause                                       ; spinlock pause
                      bswap rax                                   ; reverse byte order
                      rol rax, 7                                  ; rotate left by prime
                      xor r11, rax                                ; xor rax to r11
                      rol r11, 5                                  ; rotate left by prime
                      sub rcx, 1                                  ; decrement counter
                  
                      jnz @B
                      mov rax, r11                                ; return the value in r11
                      ret
                  
                   reseed endp
                  
                   STACKFRAME
                  
                  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  hutch at movsd dot com
                  The MASM Forum

                  www.masm32.com

                  Comment


                  • #10
                    Originally posted by Steve
                    The level of literacy here is truly mind boggling.
                    Thanks, Steve, I feel sure that Stuart will appreciate the compliment as well.

                    what is the bias against RDTSC BSWAP and MOV ?
                    What bias? I cannot be bothered to search for it but you introduced the '! rdtsc, ! bswap eax' combo quite a while ago and reintroduced it again only recently. You then use it again in the opening post's link. That is what I meant with "flogging the death out of ...".

                    In one of Melissa O'Neill's blogs she looks at seeding, and she cites, among others, using rdtsc. Using bswap made sense to me, destroying the sequential aspect of rdtsc, and I could not understand the objections to it.

                    but why call a random number generator as a seeding source
                    That is not unusual: Intel's RdSeed was developed to do just that; many folk use the KISS generator to seed Mersenne Twister. My modus operandi is to use a CPRNG to seed a PRNG.

                    when you can do it with two instructions ?
                    You are using two instructions with the core idea but you are using quite a few more in your link.

                    Surely code economy (read small and fast) has some mileage here.
                    I don't think that 'RtlGenRandom( szClassName, 64 )' will leave you short of RAM and, on my machine, takes 13.5µs to execute so it is not exactly dragging its feet.

                    now here is a suggestion, take up assembler programming
                    Funny you should suggest that because forty years ago, next year, I did just that using machine code on the ZX81. I say machine code because at the time I did not have an assembler and typed in hex code. Eventually, I got Hisoft Devpac. More than seventy-five per cent of my coding on the Atari ST was written in assembler. Admittedly, I have not done a great deal with Intel stuff at PowerBASIC and I do even less at FreeBASIC because the binaries produced by the C optimizing compiler are so fast. So don't assume from your ivory tower that the rest of haven't a clue about assembler programming.

                    Brought to you from my magnificent 12 core Xeon which I have just about finished, hardware is up and going, just added another 4tb disk and all of the software is close to configured.
                    What does that have to do with the price of bacon?

                    Am I being over critical? I don't think so as I reckon your post #8 is not up to your usual standards.

                    However, I like the look of your post #9.

                    FreeBASIC has an inline 64-bit assembler and knows what a 64-bit unsigned integer is, so I could put rax into a function unsigned __int64 return value.

                    Comment


                    • #11
                      Steve, question: What is 'mov rax, 100 ; out of range number' for?

                      Comment


                      • #12
                        RAX - 64 bit register A
                        EAX - 32 bit register A (low half of RAX)
                        AX - 16 bit register A (low 16 bits of EAX)
                        AL - low 8 bit register (low half of AX)
                        AH - high 8 bit register (high half of AX)
                        same pattern for B, C and D
                        see diagram above https://en.wikipedia.org/wiki/X86#64-bit part of page. There are more registers.

                        (David, maybe others looking too, so I put more than you probably need.)

                        ((or did you mean the out of range part??? ))
                        Dale

                        Comment


                        • #13
                          Originally posted by David Roberts View Post
                          Using bswap made sense to me, destroying the sequential aspect of rdtsc, and I could not understand the objections to it.
                          WIth a register that is rotating through the 2^32 values in the DWORD about every 2 seconds and a single request for a seed for the RNG, what is the "sequential" aspect you are so concerned about?

                          Unless you are re-seeding the RNG repeatedly in a very short time frame there is no "sequential aspect".. The BSWAP is superfluous in this use case



                          Comment


                          • #14
                            > What bias? I cannot be bothered to search for it but you introduced the '! rdtsc, ! bswap eax' combo quite a while ago and reintroduced it again only recently. You then use it again in the opening post's link. That is what I meant with "flogging the death out of ...".

                            You appear to have missed what the posted algo does, it seeded the PB randomize so that the following loop could produce a sequence of random characters. It used the old PB rnd so it was easier to understand and was very simple code.

                            The seeding is small, fast and extremely difficult to duplicate yet there are many bigger, slower techniques that could be use, get the result from GetTickCount and BSWAP it but that involves an API call. There are many other methods but I doubt they are that small or as fast as the RDTSC BSWAP combination.

                            Now as far as "flogging to death", how many unintelligible acronyms have I heard out of you over the last few years ?

                            As far as the choice of mnemonics, your whining seems to have missed the rest of the mnemonics.
                            Code:
                              MOV
                              ADD
                              CMP
                              JNE
                            I can assure you I HAVE flogged all of these but not to death, just mercilessly for years and there are many others.

                            Now the only reason I can think of is that both you and Stuart are suffering lockdown boredom, can't you find a good movie to watch, roll on down to your local pub and do injustice to a few pints, go for a walk in the park or something productive ?
                            hutch at movsd dot com
                            The MASM Forum

                            www.masm32.com

                            Comment


                            • #15
                              Thanks, Dale. I have had this jpg on my desktop since I joined FreeBASIC.

                              Click image for larger version

Name:	Registers.jpg
Views:	130
Size:	47.6 KB
ID:	801425

                              Stuart:

                              It is funny how the brain works. We don't buy into a statement. We don't buy into another statement. Another statement comes along and we buy into it.

                              The BSWAP is superfluous in this use case
                              You have persuaded me.

                              Comment


                              • #16
                                Here is a variation that should keep you happy as long as you don't look at the macro.
                                Code:
                                ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                                
                                  MACRO Dont_Call_This_Reseeding
                                    MACROTEMP nvar
                                    LOCAL nvar as DWORD
                                    ! rdtsc
                                    ! bswap eax
                                    ! mov nvar, eax
                                    randomize nvar
                                  END MACRO
                                
                                FUNCTION randstr(ByVal ccnt as DWORD) as STRING
                                
                                    #REGISTER NONE
                                
                                    LOCAL cnt as DWORD
                                
                                    Dont_Call_This_Reseeding
                                
                                    src$ = ""
                                
                                    cnt = 0
                                    ! mov esi, ccnt
                                
                                  lbl:
                                    src$ = src$ + chr$(rnd(0,255))
                                    ! add cnt, 1
                                    ! cmp cnt, esi
                                    ! jne lbl
                                
                                    FUNCTION = src$
                                
                                End FUNCTION
                                
                                ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                                hutch at movsd dot com
                                The MASM Forum

                                www.masm32.com

                                Comment


                                • #17
                                  OK, I finally looked at the algorithm link.

                                  WTF?
                                  Using RND(0 to 255) to generate a Windows class name (NULL-TERMINATED) string?

                                  1 in 256 times, you generate a NULL string.
                                  I'll leave it up to others to work out how many times you end up with a 1,2 or 3 character string

                                  Added: Ok, I decided to do it myself;
                                  '
                                  Code:
                                  #COMPILE EXE
                                  FUNCTION PBMAIN() AS LONG
                                      LOCAL x,y AS LONG
                                      LOCAL q AS QUAD
                                      LOCAL s AS STRING
                                      LOCAL sWinName AS STRINGZ*65
                                      LOCAL hTxt AS DWORD
                                      DIM aLen(0 TO 64) AS LONG
                                      TIX q
                                      RANDOMIZE LO(DWORD,q)
                                      FOR x = 1 TO 10000
                                          s=""
                                          FOR y = 1 TO 64
                                              s+=CHR$(RND(0,255))
                                          NEXT
                                          sWinName = s
                                          INCR aLen(LEN(sWinName))
                                       NEXT
                                      s="Len  Count" & $LF
                                      FOR x = 0 TO 3
                                          s += STR$(x) & $TAB & STR$(aLen(x)) & $LF
                                      NEXT
                                      ? s
                                  
                                  END FUNCTION
                                  '

                                  Comment


                                  • #18
                                    Stuart,

                                    > WIth a register that is rotating through the 2^32 values in the DWORD about every 2 seconds and a single request for a seed for the RNG, what is the "sequential" aspect you are so concerned about?

                                    You have misunderstood BSWAP. It does not rotate the register, it reverses the byte order.

                                    00112233 becomes 33221100
                                    hutch at movsd dot com
                                    The MASM Forum

                                    www.masm32.com

                                    Comment


                                    • #19
                                      WTF?
                                      Using CHR$(0 to 255) to generate a Windows class name (NULL-TERMINATED) string?
                                      WTF,
                                      The most you can lose is one trailing zero and remember, its being written to a basic dynamic string that can contain ascii zeros.


                                      hutch at movsd dot com
                                      The MASM Forum

                                      www.masm32.com

                                      Comment


                                      • #20
                                        Originally posted by Steve
                                        You appear to have missed what the posted algo does
                                        As John Wayne would say: "The hell I did!"

                                        how many unintelligible acronyms have I heard out of you over the last few years ?
                                        Unintelligible acronyms? None. If ever I use an acronym, like NIST for example, I would write National Institute of Standards and Technology (NIST) and then use NIST thereafter. I think you meant something else - not acronyms. Perhaps you meant metaphor. I am from Yorkshire, England, and we are very metaphorical. Have a conversation with a Yorky and it is like talking to a poet. Having said that, you'd probably end up scratching your head until the cows come home.

                                        your whining seems to have missed the rest of the mnemonics
                                        Whining? Your perception is shot to pieces.

                                        Now the only reason I can think of is that both you and Stuart are suffering lockdown boredom ...
                                        If that is the only reason I suggest that you go for a walk in the park and give it a bit more thought.

                                        I looked at your post #16 and it did not do a lot for me.

                                        as long as you don't look at the macro.
                                        I don't have a major issue with your idea - never did have. It is just another tool in the toolbox. As I keep saying I use a CPRNG to seed a PRNG. I use an API - so what? If two pieces of code are below the noise floor it matters not which is the fastest.


                                        Comment

                                        Working...
                                        X