Skip to content

Instantly share code, notes, and snippets.

@vanhecke
Created September 8, 2020 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanhecke/d6c1cf24c5671a737d9eb1d8f4426167 to your computer and use it in GitHub Desktop.
Save vanhecke/d6c1cf24c5671a737d9eb1d8f4426167 to your computer and use it in GitHub Desktop.
require "digest/md5"
module Captcha
def get_text(secret, random, alphabet = "abcdefghijklmnopqrstuvwxyz", character_count = 6)
if character_count < 1 || character_count > 16
raise "Character count of #{character_count} is outside the range of 1-16"
end
input = "#{secret}#{random}"
if alphabet != "abcdefghijklmnopqrstuvwxyz" || character_count != 6
input += ":#{alphabet}:#{character_count}"
end
bytes = Digest::MD5.digest(input)[0..5]
text = ""
bytes.each do |byte|
text += alphabet[byte % alphabet.size]
end
text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment