tags:

views:

30

answers:

2

I need to generate boundary for a multi-part upload

  post << "--#{BOUNDARY}\r\n"
  post << "Content-Disposition: form-data; name=\"datafile\"; filename=\"#{filename}\"\r\n"
  post << "Content-Type: text/plain\r\n"
  post << "\r\n"
  post << file
  post << "\r\n--#{BOUNDARY}--\r\n"

The BOUNDARY need to be a random string (not present in the file).

In rails, I could do SecureRandom.hex(10)

Who can I do it without loading activesupport?

A: 

Last time I used MD5 on rand like this:

require 'md5'
random_string = MD5.md5(rand(1234567).to_s).to_s
retro
+1  A: 

If you need a random alphanumeric string, use something like:

rand(10000000000000).floor.to_s(36)

This will make a random number (change the multiplier to make the string longer) and represent it in radix 36 (10 numbers + 26 letters).

For a Base64 string, you could do something like

require 'base64'
Base64.encode64(rand(10000000000000).to_s).chomp("=\n")

If you need strings of a fixed length, play with the random number range you're supplying, using something like 1000000 + rand(10000000).

micho
rand(10**30).to_s(36) did the trick!
unixcharles