Sha256: d2ffadb77782b14f5253ba6cd302a255b29937e45f10e2e580c5dac39712efdf

Contents?: true

Size: 770 Bytes

Versions: 1

Compression:

Stored size: 770 Bytes

Contents

module SmartId
  module Utils
    class VerificationCodeCalculator
      ##
      # The Verification Code (VC) is computed as:
      #
      # integer(SHA256(hash)[−2:−1]) 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.

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_id-0.1.0 lib/smart_id/utils/verification_code_calculator.rb