Class: StunClient::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/stun-client/generic/utilities.rb

Overview

Contains useful functions used by different parts of the STUN client implementation. These functions can act independently of the context.

Class Method Summary collapse

Class Method Details

.generate_txid(bits) ⇒ Object

Generates a random transaction ID. It contains the characters 0-9 and a-f.

Parameters:

  • bits (Integer)

    The number of bits the transaction ID should be long.



10
11
12
13
14
15
16
# File 'lib/stun-client/generic/utilities.rb', line 10

def self.generate_txid bits
  bytes = bits / 8
  raise 'A TxID with bits not divisible by eight is not possible.' if bits % 8 != 0

  bytes /= 2  # as .hex doubles the length
  return SecureRandom.hex(bytes)
end

.sanitize_string(str) ⇒ Object

Attempts to sanitize a string before it is processed further. This tries to escape some security holes. For sanitizing the function ‘dump` is used.

Parameters:

  • str (String)

    String which is to be sanitized.

Returns:

  • Sanitize string



24
25
26
# File 'lib/stun-client/generic/utilities.rb', line 24

def self.sanitize_string str
  return str.dump[1...-1]
end