vb.net - How to generate a 26-character hex string that equals to 106 bits and ((53 Ones - 53 Zeros) in binary) -


i looking way generate hexadecimal string equals out 106 bits, more fifty 3 1's , fifty 3 0's after each hex char converted binary , added together. i'd keep random possible considering parameters of request. how go keeping eye on construction of string equals out way want?

for example:

(a8c05779f8934b14ce96f8aa93) = (1010 1000 1100 0000 0101 0111 0111 1001 1111 1000 1001 0011 0100  1011 0001 0100 1100 1110 1001 0110 1111 1000 1010 1010 1001 0011) 

you can place 52 ones randomly in 104 bit number keeping track of how many ones has been placed , calculate probability next digit should one. first digit has 1/2 probability (52/104), second digit has 51/103 or 52/103 probability depending on first digit was, , on.

put bits in buffer, , when full (four bits), makes hexadecimal digit can add string:

dim rnd new random() dim bin new stringbuilder() dim buf integer = 0, buflen integer = 0, left integer = 52 integer = 104 1 step -1   buf <<= 1   if rnd.next(i) < left     buf += 1     left -= 1   end if   buflen += 1   if buflen = 4     bin.append("0123456789abcdef"(buf))     buflen = 0     buf = 0   end if next dim b string = bin.tostring() 

to make 106 bit value, change these lines:

dim buf integer = 0, buflen integer = 0, left integer = 53 integer = 106 1 step -1 

the resulting string still 26 characters, 2 bits in buf variable. has value between 0 , 3 can use create 27th character, done.


to add 22 bit hash string, can use code this:

bin.append("048c"(buf)) dim b string = bin.tostring()  dim m new system.security.cryptography.sha1managed dim hash byte() = m.computehash(encoding.utf8.getbytes(b))  'replace first 2 bits in hash bits buf hash(0) = cbyte(hash(0) , &h3f or (buf * 64)) 'append 24 bits hash b = b.substring(0, 26) + bitconverter.tostring(hash, 0, 3).replace("-", string.empty) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -