Class: Rocra

Inherits:
Object
  • Object
show all
Defined in:
lib/rocra.rb,
lib/rocra/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.generate(ocra_suite, key, counter, question, password, session_information, timestamp) ⇒ Object

This method generates an OCRA HOTP value for the given set of parameters.

truncationDigits digits

Parameters:

  • ocraSuite

    the OCRA Suite

  • key

    the shared secret, HEX encoded

  • counter

    the counter that changes on a per use basis, HEX encoded

  • question

    the challenge question, HEX encoded

  • password

    a password that can be used, HEX encoded

  • sessionInformation

    Static information that identifies the current session, Hex encoded

  • timestamp

    a value that reflects a time

Returns:

  • A numeric String in base 10 that includes



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rocra.rb', line 27

def generate(ocra_suite, key, counter, question, password, session_information, timestamp)

  code_digits = 0
  crypto = ""
  result = nil
  ocra_suite_length = ocra_suite.length
  counter_length = 0
  question_length = 0
  password_length = 0

  session_information_length = 0
  timestamp_length = 0

  # How many digits should we return
  components = ocra_suite.split(':')
  cryptoFunction = components[1]
  dataInput = components[2].downcase # lower here so we can do case insensitive comparisons

  crypto = 'sha1'   if cryptoFunction.downcase.include?('sha1')
  crypto = 'sha256' if cryptoFunction.downcase.include?('sha256')
  crypto = 'sha512' if cryptoFunction.downcase.include?('sha512')

  code_digits = cryptoFunction.split('-').last

  # The size of the byte array message to be encrypted

  # Counter
  if dataInput[0,1] == "c"
    # Fix the length of the HEX string
    counter_length=8
    counter = counter.rjust(16, '0')
  end

  # Question
  if dataInput[0,1] == "q" || dataInput.include?('-q')
    question = question.ljust(256, '0')
    question_length = 128
  end

  # Password
  if dataInput.include?("psha1")
    password = password.ljust(40, '0')
    password_length = 20
  end

  if dataInput.include?("psha256")
    password = password.ljust(64, '0')
    password_length = 32
  end

  if dataInput.include?("psha512")
    password = password.ljust(128, '0')
    password_length = 64
  end

  # session_information
  if dataInput.include?("s064")
    session_information = session_information.ljust(128, '0')
    session_information_length = 64
  end

  if dataInput.include?("s128")
    session_information = session_information.ljust(256, '0')
    session_information_length = 128
  end

  if dataInput.include?("s256")
    session_information = session_information.ljust(512, '0')
    session_information_length = 256
  end

  if dataInput.include?("s512")
    session_information = session_information.ljust(128, '0')
    session_information_length = 64
  end

  # TimeStamp
  if dataInput[0,1] == "t" || dataInput.include?("-t")
    timestamp = timestamp.rjust(16, '0')
    timestamp_length = 8
  end

  # Put the bytes of "ocra_suite" parameters into the message
  length = ocra_suite_length +
    counter_length +
    question_length +
    password_length +
    session_information_length +
    timestamp_length + 1
  msg = "\0" * length
  msg[0, ocra_suite.length] = ocra_suite

  # Delimiter
  # msg[ocra_suite.length] = hex2str("0")

  # Put the bytes of "Counter" to the message
  # Input is HEX encoded
  if counter_length > 0
    pos = ocra_suite_length + 1
    msg[pos, counter.length] = hex2str(counter)
  end

  # Put the bytes of "question" to the message
  # Input is text encoded
  if question_length > 0
    pos = ocra_suite_length + 1 + counter_length
    msg[pos, question.length] = hex2str(question)
  end

  # Put the bytes of "password" to the message
  # Input is HEX encoded
  if password_length > 0
    pos = ocra_suite_length + 1 + counter_length + question_length
    msg[pos, password.length] = hex2str(password)
  end

  # Put the bytes of "session_information" to the message
  # Input is text encoded
  if session_information_length > 0
    pos = ocra_suite_length + 1 + counter_length + question_length + password_length
    msg[pos, session_information.length] = hex2str(session_information)
  end

  # Put the bytes of "time" to the message
  # Input is text value of minutes
  if timestamp_length > 0
    pos = ocra_suite_length + 1 + counter_length + question_length +
      password_length + session_information_length
    msg[pos, timestamp.length] = hex2str(timestamp)
  end

  byteKey = hex2str(key)
  hash = hmac_sha1(crypto, byteKey, msg)
  oath_truncate(hash, code_digits)

end