Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Created April 1, 2021 11:49
Show Gist options
  • Select an option

  • Save benoittgt/950a56c6d628552fc64a642579b38d0f to your computer and use it in GitHub Desktop.

Select an option

Save benoittgt/950a56c6d628552fc64a642579b38d0f to your computer and use it in GitHub Desktop.
Test TOTP collision
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rotp"
gem 'activesupport'
end
class Otpable
DRIFT = 20# 10 days
attr_accessor(:totp, :proof)
def initialize
@totp = ROTP::TOTP.new(ROTP::Base32.random, issuer: 'Club service')
end
def generate_otp
@proof = totp.now
end
def verify_otp(drift_behind: DRIFT)
totp.verify(proof, drift_behind: drift_behind)
end
end
require 'active_support/testing/time_helpers'
include ActiveSupport::Testing::TimeHelpers
100000000.times do |i|
optable = Otpable.new
optable.generate_otp
print "."
raise "SHOULD NOT BE NIL" if optable.verify_otp.nil?
travel_to(Time.now + (Otpable::DRIFT * 2)) do
raise "SHOULD BE NIL" unless optable.verify_otp.nil?
end
rescue => e
puts
puts "Failed at #{i} times"
puts
raise e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment