Guest User

Untitled

a guest
Jul 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. /* compiles with:
  2. gcc -O1 -Wall -Wextra -std=c99 -lssl -lcrypto -lcrypt mkpasswd.c -o mkpasswd
  3.  
  4. On RedHat/CentOS it requires openssl-devel and gcc
  5. */
  6.  
  7. #define _XOPEN_SOURCE 500
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <assert.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14.  
  15. #include <openssl/rand.h>
  16. #include <openssl/err.h>
  17. #include <openssl/bio.h>
  18. #include <openssl/evp.h>
  19.  
  20. #define MY_ENTROPY_BITS 512
  21. #define SALT_BITS 128
  22. #define CRYPT_PASS_SUFFIX "$6$"
  23.  
  24. int b64_op(const void *in, int in_len, char *out, int out_len, int op) {
  25. int ret = 0;
  26. BIO *b64 = BIO_new(BIO_f_base64());
  27. BIO *bio = BIO_new(BIO_s_mem());
  28. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  29. BIO_push(b64, bio);
  30. if (!op) {
  31. ret = BIO_write(b64, in, in_len);
  32. BIO_flush(b64);
  33. if (ret > 0) {
  34. ret = BIO_read(bio, out, out_len);
  35. }
  36. } else {
  37. ret = BIO_write(bio, in, in_len);
  38. BIO_flush(bio);
  39. if (ret) {
  40. ret = BIO_read(b64, out, out_len);
  41. }
  42. }
  43. BIO_free(b64);
  44. BIO_free(bio);
  45. return ret;
  46. }
  47.  
  48. int main() {
  49. uint8_t salt_bytes=SALT_BITS >> 3;
  50. uint8_t entropy_bytes=MY_ENTROPY_BITS >> 3;
  51. uint8_t salt_buffer[salt_bytes];
  52.  
  53. uint8_t i,salt_len;
  54. char *gen_salt,*password,*salt;
  55.  
  56. printf("Waiting for enough entropy to be available\n");
  57. int rc = RAND_load_file("/dev/random", entropy_bytes);
  58. if (rc != entropy_bytes) {
  59. printf("Error collecting random bytes\n");
  60. return -1;
  61. }
  62.  
  63. rc = RAND_bytes(salt_buffer, salt_bytes);
  64. unsigned long err = ERR_get_error();
  65.  
  66. if (rc != 1) {
  67. printf("Error generating salt bytes\n");
  68. return err;
  69. }
  70.  
  71. gen_salt=(char *)calloc(salt_bytes+1,sizeof(char));
  72. if (!gen_salt) {
  73. perror("calloc");
  74. return 1;
  75. }
  76.  
  77. rc=b64_op(salt_buffer,salt_bytes,gen_salt,salt_bytes,0);
  78. for (i=0;i<salt_bytes;i++)
  79. if ((gen_salt[i]=='+')||(gen_salt[i]=='=')) gen_salt[i]='.';
  80.  
  81. salt_len=strlen(CRYPT_PASS_SUFFIX)+strlen(gen_salt);
  82. salt=(char *)calloc(salt_len+1,1);
  83. if (!salt) {
  84. perror("calloc");
  85. free(gen_salt);
  86. return 1;
  87. }
  88.  
  89. snprintf(salt,salt_len,"%s%s",CRYPT_PASS_SUFFIX,salt);
  90.  
  91. password = getpass("Enter your password: ");
  92. if (!password) {
  93. perror("getpass");
  94. free(gen_salt);
  95. free(salt);
  96. return 1;
  97. }
  98.  
  99. char *sha512_pass=crypt(password,salt);
  100. free(gen_salt);
  101. free(salt);
  102.  
  103. if (!sha512_pass) {
  104. perror("crypt");
  105. return 1;
  106. }
  107.  
  108. printf("SHA512 password hash: %s\n",sha512_pass);
  109. return 0;
  110. }
Add Comment
Please, Sign In to add comment