Advertisement
campos20

Untitled

May 20th, 2020
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Gera senha aleatoria
  6. // Autor Alexandre Campos
  7.  
  8. // Gera 1 numero aleatorio ate um limite
  9. int numero_aleatorio(int maximo)
  10. {
  11.     return random() % maximo;
  12. }
  13.  
  14. char * gera_senha(int tamanho){
  15.     // Aloca o espaco
  16.     char * resultado = malloc(tamanho * sizeof(char));
  17.  
  18.     char permitidos[] = "23456789qwertyuiopasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM!@#$";
  19.  
  20.     // strlen calcule o tamanho de uma string data
  21.     int tamanho_dos_permitidos = strlen(permitidos);
  22.  
  23.     // Preencher a senha com caracteres aleatorios
  24.     for (int i=0; i<tamanho; i++){
  25.  
  26.         // Gera posicao aleatoria
  27.         int posicao_aleatoria = numero_aleatorio(tamanho_dos_permitidos);
  28.  
  29.         // Na posicao i da senha, coloca um caracter da posicao aleatoria
  30.         resultado[i] = permitidos[posicao_aleatoria];
  31.     }
  32.  
  33.     return resultado;
  34. }
  35.  
  36. int main()
  37. {
  38.     // Chamar 1 vez
  39.     srand(time(NULL));
  40.  
  41.     // Declaracao
  42.     int tamanho_da_senha = 10;
  43.     char * senha = gera_senha(tamanho_da_senha);
  44.  
  45.     // Exibe a senha
  46.     printf("Senha: %s\n", senha);
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement