crypt.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * crypt() for uClibc
  4. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #define __FORCE_GLIBC
  8. #include <crypt.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "libcrypt.h"
  13. typedef char *(*crypt_impl_f)(const unsigned char *pw, const unsigned char *salt);
  14. static const struct {
  15. const char *salt_pfx;
  16. const crypt_impl_f crypt_impl;
  17. } crypt_impl_tab[] = {
  18. { "$1$", __md5_crypt },
  19. #ifdef __UCLIBC_HAS_SHA256_CRYPT_IMPL__
  20. { "$5$", __sha256_crypt },
  21. #endif
  22. #ifdef __UCLIBC_HAS_SHA512_CRYPT_IMPL__
  23. { "$6$", __sha512_crypt },
  24. #endif
  25. { NULL, __des_crypt },
  26. };
  27. char *crypt(const char *key, const char *salt)
  28. {
  29. const unsigned char *ukey = (const unsigned char *)key;
  30. const unsigned char *usalt = (const unsigned char *)salt;
  31. size_t i;
  32. for (i = 0; i < ARRAY_SIZE(crypt_impl_tab); i++) {
  33. if (crypt_impl_tab[i].salt_pfx != NULL &&
  34. strncmp(crypt_impl_tab[i].salt_pfx, salt, strlen(crypt_impl_tab[i].salt_pfx)))
  35. continue;
  36. return crypt_impl_tab[i].crypt_impl(ukey, usalt);
  37. }
  38. /* no crypt implementation was found, set errno to ENOSYS and return NULL */
  39. __set_errno(ENOSYS);
  40. return NULL;
  41. }