Class: SmartId::Utils::VerificationCodeCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_id/utils/verification_code_calculator.rb

Class Method Summary collapse

Class Method Details

.calculate(digest) ⇒ Object

The Verification Code (VC) is computed as:

integer(SHA256(hash)) mod 10000

where we take SHA256 result, extract 2 rightmost bytes from it, interpret them as a big-endian unsigned short and take the last 4 digits in decimal for display.

SHA256 is always used here, no matter what was the algorithm used to calculate hash.



14
15
16
17
18
19
20
21
# File 'lib/smart_id/utils/verification_code_calculator.rb', line 14

def self.calculate(digest)
  rightmost_bytes = digest[-2..-1]
  int = rightmost_bytes.unpack('n*')[0]
  paddable_string = (int % 10000).to_s.chars.last(4).join
  pad = 4 - paddable_string.length
  
  "0" * pad + paddable_string
end