[PATCH 1/2] libcrypt: make crypt() more modular

Philip Craig philipjcraig at gmail.com
Fri Nov 18 03:16:55 UTC 2011


From: William Pitcock <nenolod at dereferenced.org>
Sent: Wed 16 Nov 2011 21:27:48 EST

>  char *crypt(const char *key, const char *salt)
>  {
> -	/* First, check if we are supposed to be using the MD5 replacement
> -	 * instead of DES...  */
> -	if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$')
> -		return __md5_crypt((unsigned char*)key, (unsigned char*)salt);
> -	else
> -		return __des_crypt((unsigned char*)key, (unsigned char*)salt);
> +	unsigned int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(crypt_impl_tab); i++)
> +	{
> +		if (crypt_impl_tab[i].salt_pfx &&
> +			strncmp(crypt_impl_tab[i].salt_pfx, salt, strlen(crypt_impl_tab[i].salt_pfx)))
> +			continue;
> +
> +		return crypt_impl_tab[i].crypt_impl((unsigned char *) key, (unsigned char *) salt);
> +	}
> +
> +	/* this should never happen, but just incase... */
> +	__set_errno(ENOSYS);
> +	return NULL;
>  }

You can make that a bit simpler and get rid of that ugly end case:

char *crypt(const char *key, const char *salt)
{
	const struct crypt_impl *p;

	for (p = crypt_impl_tab; p->salt_pfx; p++)
		if (strncmp(p->salt_pfx, salt, strlen(p->salt_pfx)) == 0)
			break;

	return p->crypt_impl((unsigned char *)key, (unsigned char *)salt);
}


More information about the uClibc mailing list