class SecureRandom

class SecureRandom

lib/secure_random.tya:2

SecureRandom provides the secure_random/SecureRandom standard library API.

Source
# SecureRandom provides the secure_random/SecureRandom standard library API.
class SecureRandom
  # SecureRandom.base64 provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  base64: n ->
    alphabet = SecureRandom().base64_alphabet()
    out = ""
    i = 0
    while i + 3 <= n
      b1 = secure_random_int(0, 255)
      b2 = secure_random_int(0, 255)
      b3 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16 + b2 / 16.to_i()
      g3 = b2 % 16 * 4 + b3 / 64.to_i()
      g4 = b3 % 64
      out = out + alphabet[g1] + alphabet[g2] + alphabet[g3] + alphabet[g4]
      i = i + 3
    rem = n - i
    if rem == 1
      b1 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16
      out = out + alphabet[g1] + alphabet[g2] + "=="
    elseif rem == 2
      b1 = secure_random_int(0, 255)
      b2 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16 + b2 / 16.to_i()
      g3 = b2 % 16 * 4
      out = out + alphabet[g1] + alphabet[g2] + alphabet[g3] + "="
    out

  # SecureRandom.base64_alphabet provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  base64_alphabet: ->
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

  # SecureRandom.bytes provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  bytes: n ->
    secure_random_bytes(n)

  # SecureRandom.hex provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return String the resulting value.
  hex: n ->
    out = ""
    i = 0
    while i < n
      b = secure_random_int(0, 255)
      out = out
        + SecureRandom().hex_digit(b / 16.to_i())
        + SecureRandom().hex_digit(b % 16)
      i = i + 1
    out

  # SecureRandom.hex_byte provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  hex_byte: ->
    b = secure_random_int(0, 255)
    SecureRandom().hex_digit(b / 16.to_i()) + SecureRandom().hex_digit(b % 16)

  # SecureRandom.hex_digit provides the secure_random/SecureRandom standard library operation.
  # @param d Any d value.
  # @return Any the resulting value.
  hex_digit: d ->
    if d < 10
      return chr(48 + d)
    chr(87 + d)

  # SecureRandom.int provides the secure_random/SecureRandom standard library operation.
  # @param min Any min value.
  # @param max Any max value.
  # @return Any the resulting value.
  int: min, max ->
    secure_random_int(min, max)

  # SecureRandom.uuid provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  uuid: ->
    bs = []
    i = 0
    while i < 16
      bs.push(secure_random_int(0, 255))
      i = i + 1
    bs[6] = bs[6] % 16 + 64
    bs[8] = bs[8] % 64 + 128
    parts = ""
    i = 0
    while i < 16
      b = bs[i]
      parts = parts
        + SecureRandom().hex_digit(b / 16.to_i())
        + SecureRandom().hex_digit(b % 16)
      if i == 3 or i == 5 or i == 7 or i == 9
        parts = parts + "-"
      i = i + 1
    parts

Methods

base64

SecureRandom.base64(n)

lib/secure_random.tya:6

SecureRandom.base64 provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.base64 provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  base64: n ->
    alphabet = SecureRandom().base64_alphabet()
    out = ""
    i = 0
    while i + 3 <= n
      b1 = secure_random_int(0, 255)
      b2 = secure_random_int(0, 255)
      b3 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16 + b2 / 16.to_i()
      g3 = b2 % 16 * 4 + b3 / 64.to_i()
      g4 = b3 % 64
      out = out + alphabet[g1] + alphabet[g2] + alphabet[g3] + alphabet[g4]
      i = i + 3
    rem = n - i
    if rem == 1
      b1 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16
      out = out + alphabet[g1] + alphabet[g2] + "=="
    elseif rem == 2
      b1 = secure_random_int(0, 255)
      b2 = secure_random_int(0, 255)
      g1 = b1 / 4.to_i()
      g2 = b1 % 4 * 16 + b2 / 16.to_i()
      g3 = b2 % 16 * 4
      out = out + alphabet[g1] + alphabet[g2] + alphabet[g3] + "="
    out

base64_alphabet

SecureRandom.base64_alphabet()

lib/secure_random.tya:37

SecureRandom.base64_alphabet provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.base64_alphabet provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  base64_alphabet: ->
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

bytes

SecureRandom.bytes(n)

lib/secure_random.tya:43

SecureRandom.bytes provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.bytes provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  bytes: n ->
    secure_random_bytes(n)

hex

SecureRandom.hex(n)

lib/secure_random.tya:49

SecureRandom.hex provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.hex provides the secure_random/SecureRandom standard library operation.
  # @param n Int n value.
  # @return String the resulting value.
  hex: n ->
    out = ""
    i = 0
    while i < n
      b = secure_random_int(0, 255)
      out = out
        + SecureRandom().hex_digit(b / 16.to_i())
        + SecureRandom().hex_digit(b % 16)
      i = i + 1
    out

hex_byte

SecureRandom.hex_byte()

lib/secure_random.tya:62

SecureRandom.hex_byte provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.hex_byte provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  hex_byte: ->
    b = secure_random_int(0, 255)
    SecureRandom().hex_digit(b / 16.to_i()) + SecureRandom().hex_digit(b % 16)

hex_digit

SecureRandom.hex_digit(d)

lib/secure_random.tya:69

SecureRandom.hex_digit provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.hex_digit provides the secure_random/SecureRandom standard library operation.
  # @param d Any d value.
  # @return Any the resulting value.
  hex_digit: d ->
    if d < 10
      return chr(48 + d)
    chr(87 + d)

int

SecureRandom.int(min, max)

lib/secure_random.tya:78

SecureRandom.int provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.int provides the secure_random/SecureRandom standard library operation.
  # @param min Any min value.
  # @param max Any max value.
  # @return Any the resulting value.
  int: min, max ->
    secure_random_int(min, max)

uuid

SecureRandom.uuid()

lib/secure_random.tya:83

SecureRandom.uuid provides the secure_random/SecureRandom standard library operation.

Source
  # SecureRandom.uuid provides the secure_random/SecureRandom standard library operation.
  # @return Any the resulting value.
  uuid: ->
    bs = []
    i = 0
    while i < 16
      bs.push(secure_random_int(0, 255))
      i = i + 1
    bs[6] = bs[6] % 16 + 64
    bs[8] = bs[8] % 64 + 128
    parts = ""
    i = 0
    while i < 16
      b = bs[i]
      parts = parts
        + SecureRandom().hex_digit(b / 16.to_i())
        + SecureRandom().hex_digit(b % 16)
      if i == 3 or i == 5 or i == 7 or i == 9
        parts = parts + "-"
      i = i + 1
    parts