A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) je test, který má určit, zda je uživatel člověk nebo ne.
Úkolem je tedy pokaždé vygenerovat unikátní CAPTCHA a zjistit, zda je uživatel člověk nebo ne, tím, že požádáte uživatele, aby zadal stejnou CAPTCHA, jaká byla vygenerována automaticky, a zkontroluje vstup uživatele pomocí vygenerované CAPTCHA.
Příklady:
CAPTCHA: x9Pm72se Input: x9Pm62es Output: CAPTCHA Not Matched CAPTCHA: cF3yl9T4 Input: cF3yl9T4 Output: CAPTCHA Matched
Sada znaků pro generování CAPTCHA je uložena v poli znaků chrs[], které obsahuje (a-z A-Z 0-9), takže velikost chrs[] je 62.
Chcete-li vygenerovat jedinečný CAPTCHA pokaždé, když je vygenerováno náhodné číslo pomocí funkce rand() (rand()%62), která generuje náhodné číslo mezi 0 až 61 a vygenerované náhodné číslo se vezme jako index do pole znaků chrs[], vygeneruje se nový znak captcha[] a tato smyčka se spustí n (délka CAPTCHA) krát, aby se vygeneroval CAPTCHA dané délky.
Algoritmus:
- Nejprve deklarujte a definujte funkci checkCaptcha(), která přebírá dva řetězcové parametry a vrací booleovskou hodnotu.
- V rámci funkce checkCaptcha() porovnejte dva parametry řetězce pomocí funkce Compare() a vraťte true, pokud jsou stejné; jinak vrátí false.
- Deklarujte a definujte funkci createCaptcha(), která přebírá celočíselný parametr a vrací hodnotu řetězce.
- V rámci funkce createCaptcha() inicializujte časovou proměnnou pomocí funkce time() a nasaďte generátor náhodných čísel pomocí funkce srand().
- Deklarujte pole znaků obsahující všechny znaky, které mají být zahrnuty do CAPTCHA, a přiřaďte jej proměnné ukazatele char.
- Vygenerujte náhodný řetězec CAPTCHA zadané délky opakovaným přidáváním náhodných znaků z pole znaků do řetězcové proměnné pomocí funkce push_back().
- Vraťte vygenerovaný řetězec CAPTCHA.
- Ve funkci main() deklarujte řetězcovou proměnnou s názvem captcha a zavolejte funkci createCaptcha() s délkou 9 pro vygenerování náhodného řetězce CAPTCHA.
- Vytiskněte vygenerovaný řetězec CAPTCHA na konzole.
// C++ program to automatically generate CAPTCHA and // verify user #include using namespace std; // Returns true if given two strings are same bool checkCaptcha(string& captcha string& user_captcha) { return captcha.compare(user_captcha) == 0; } // Generates a CAPTCHA of given length string generateCaptcha(int n) { time_t t; srand((unsigned)time(&t)); // Characters to be included char* chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHI' 'JKLMNOPQRSTUVWXYZ0123456789'; // Generate n characters from above set and // add these characters to captcha. string captcha = ''; while (n--) captcha.push_back(chrs[rand() % 62]); return captcha; } // Driver code int main() { // Generate a random CAPTCHA string captcha = generateCaptcha(9); cout << captcha; // Ask user to enter a CAPTCHA string usr_captcha; cout << 'nEnter above CAPTCHA: '; cin >> usr_captcha; // Notify user about matching status if (checkCaptcha(captcha usr_captcha)) printf('nCAPTCHA Matched'); else printf('nCAPTCHA Not Matched'); return 0; }
Java // Java pprogram to automatically generate CAPTCHA and // verify user import java.util.*; import java.io.*; class GFG { // Returns true if given two strings are same static boolean checkCaptcha(String captcha String user_captcha) { return captcha.equals(user_captcha); } // Generates a CAPTCHA of given length static String generateCaptcha(int n) { //to generate random integers in the range [0-61] Random rand = new Random(62); // Characters to be included String chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // Generate n characters from above set and // add these characters to captcha. String captcha = ''; while (n-->0){ int index = (int)(Math.random()*62); captcha+=chrs.charAt(index); } return captcha; } // Driver code public static void main(String[] args)throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Generate a random CAPTCHA String captcha = generateCaptcha(9); System.out.println(captcha); // Ask user to enter a CAPTCHA System.out.println('Enter above CAPTCHA: '); String usr_captcha = reader.readLine(); // Notify user about matching status if (checkCaptcha(captcha usr_captcha)) System.out.println('CAPTCHA Matched'); else System.out.println('CAPTCHA Not Matched'); } } // This code is contributed by shruti456rawal
Python3 # Python program to automatically generate CAPTCHA and # verify user import random # Returns true if given two strings are same def checkCaptcha(captcha user_captcha): if captcha == user_captcha: return True return False # Generates a CAPTCHA of given length def generateCaptcha(n): # Characters to be included chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # Generate n characters from above set and # add these characters to captcha. captcha = '' while (n): captcha += chrs[random.randint(1 1000) % 62] n -= 1 return captcha # Driver code # Generate a random CAPTCHA captcha = generateCaptcha(9) print(captcha) # Ask user to enter a CAPTCHA print('Enter above CAPTCHA:') usr_captcha = input() # Notify user about matching status if (checkCaptcha(captcha usr_captcha)): print('CAPTCHA Matched') else: print('CAPTCHA Not Matched') # This code is contributed by shubhamsingh10
C# using System; using System.Text; class GFG { // Returns true if given two strings are same static bool CheckCaptcha(string captcha string user_captcha) { return captcha.Equals(user_captcha); } // Generates a CAPTCHA of given length static string GenerateCaptcha(int n) { // to generate random integers in the range [0-61] Random rand = new Random(); // Characters to be included string chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // Generate n characters from above set and // add these characters to captcha. StringBuilder captcha = new StringBuilder(); while (n-- > 0) { int index = rand.Next(62); captcha.Append(chrs[index]); } return captcha.ToString(); } // Driver code static void Main(string[] args) { // Generate a random CAPTCHA string captcha = GenerateCaptcha(9); Console.WriteLine(captcha); // Ask user to enter a CAPTCHA Console.WriteLine('Enter above CAPTCHA: '); string usr_captcha = Console.ReadLine(); // Notify user about matching status if (CheckCaptcha(captcha usr_captcha)) Console.WriteLine('CAPTCHA Matched'); else Console.WriteLine('CAPTCHA Not Matched'); } }
JavaScript // JavaScript program to automatically generate CAPTCHA and // verify user // Returns true if given two strings are same function checkCaptcha(captcha user_captcha) { return captcha.localeCompare(user_captcha) == 0; } // Generates a CAPTCHA of given length function generateCaptcha(n) { // Characters to be included const chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let captcha = ''; for (let i = 0; i < n; i++) { captcha += chrs[(Math.floor(Math.random() * chrs.length))]; } return captcha; } // Driver code function main() { // Generate a random CAPTCHA const captcha = generateCaptcha(9); console.log(captcha); // Ask user to enter a CAPTCHA const usr_captcha = prompt('Enter above CAPTCHA:'); // Notify user about matching status if (checkCaptcha(captcha usr_captcha)) console.log('CAPTCHA Matched'); else console.log('CAPTCHA Not Matched'); } main();
výstup:
příklad formátu json
CAPTCHA: cF3yl9T4 Enter CAPTCHA: cF3yl9T4 CAPTCHA Matched
Časová náročnost: Na)
Prostorová složitost: O(1)
Vytvořit kvíz