Author Topic: Random string generator function  (Read 612 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8245
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Random string generator function
« on: October 26, 2020, 12:47:21 PM »
I had a use for this recently, it was for making Window class names obscure and different each time an app starts. It is also useful for registering private messages.

    PM_CHANNEL1 = RegisterWindowMessage(randstr(128))
    szClassName  = randstr(64)

What you end up with is an ugly pile of binary crap that is different each time the app starts.

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION randstr(ByVal ccnt as DWORD) as STRING

    #REGISTER NONE

    LOCAL var as DWORD
    LOCAL cnt as DWORD

    ! rdtsc
    ! bswap eax
    ! mov var, eax

    randomize var

    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
http://www.masm32.com    :biggrin:  :skrewy:

jack

  • Member
  • **
  • Posts: 76
Re: Random string generator function
« Reply #1 on: October 26, 2020, 04:57:18 PM »
hi hutch
why did you make the random number range between 0 and 255 ? , I assume that's what rnd(0,255) does?
I would have restricted the range between 32 and 127
your function works quite well, it was simple to translate to FreeBasic :)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8245
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random string generator function
« Reply #2 on: October 26, 2020, 07:41:05 PM »
Hi Jack,

Wider range (0 to 255) has more variation, I tested it with all lower case but including all of the ASCII range made the output even messier which is exactly what I wanted. Makes finding a window class string or a private message string even harder to perform.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: