Skip to content

Instantly share code, notes, and snippets.

@vanhecke

vanhecke/captcha.cr

Created Sep 8, 2020
Embed
What would you like to do?
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