crypt.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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_SHA512_CRYPT_IMPL__
  20. { "$6$", __sha512_crypt },
  21. #endif
  22. { NULL, __des_crypt },
  23. };
  24. char *crypt(const char *key, const char *salt)
  25. {
  26. const unsigned char *ukey = (const unsigned char *)key;
  27. const unsigned char *usalt = (const unsigned char *)salt;
  28. size_t i;
  29. for (i = 0; i < ARRAY_SIZE(crypt_impl_tab); i++) {
  30. if (crypt_impl_tab[i].salt_pfx != NULL &&
  31. strncmp(crypt_impl_tab[i].salt_pfx, salt, strlen(crypt_impl_tab[i].salt_pfx)))
  32. continue;
  33. return crypt_impl_tab[i].crypt_impl(ukey, usalt);
  34. }
  35. /* no crypt implementation was found, set errno to ENOSYS and return NULL */
  36. __set_errno(ENOSYS);
  37. return NULL;
  38. }