[Patches] crypt.salt()

Matt Wimer matt@cgibuilder.com
Tue, 16 May 2000 01:17:08 -0600


--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I'll make this short.  The crypt module requires a salt
value that the user currently has to gen.  I added 
crypt.salt() function that does this for them.  It
currently isn't much but its setup to scale to the
current system.


STD:
I confirm that, to the best of my knowledge and belief, this contribution
is free of any claims of third parties under copyright, patent or
other rights or interests ("claims").  To the extent that I have
any such claims, I hereby grant to CNRI a nonexclusive, irrevocable,
royalty-free, worldwide license to reproduce, distribute, perform and/or
display publicly, prepare derivative versions, and otherwise use this
contribution as part of the Python software and its related documentation,
or any derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide whether
or not to incorporate this contribution in the Python software and its
related documentation.  I further grant CNRI permission to use my name
and other identifying information provided to CNRI by me for use in
connection with the Python software and its related documentation.


PATCH attached.

--matt wimer


--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="crypt-salt.patch"

*** Python-1.6a2/Modules/cryptmodule.c	Tue Feb  1 13:12:39 2000
--- Python-1.6a2-patched/Modules/cryptmodule.c	Tue May 16 01:12:51 2000
***************
*** 9,14 ****
--- 9,90 ----
  /* Module crypt */
  
  
+ /* SALTING the crypt() is system dependant. Let them use crypt.salt()
+  * to do the salting. */
+ 
+ 
+ /* README: 
+  * Salts can look like [suffix]<common-salt>.  I define the
+  * suffix to take OSOFFSET amount of space. And the rest 
+  * of the salt to be the salt size. 
+  *
+  * OpenBSD has a suffix of $12$<chars> in its blowfish
+  * crypt(3).  This current code will be able to scale up
+  * to meat these chalanges as people become more interested
+  * in python for thier platform.
+  */
+ 
+ #define OSOFFSET	(0)	/* No offset define yet. */
+ #define SALTSIZE	(2)	/* Pretend 2 is good enough. */
+ 
+ /* The salt chars possible. Broken up so you can see they are corret. */
+ static char salter[] = 
+ 	"0123456789"
+ 	"abcdefghijklmnopqrstuvwxyz"
+ 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ 	"./";				/* These are possible as well. */
+ 
+ /* Each system has its own salter type for specifing
+  * long crypted passwds. We'll just define that crypt.salt()
+  * generated the standard two letter salt if we can't guess
+  * the correct salter to use. 
+  */
+ static PyObject * crypt_salt(self, args)
+ 	PyObject *self, *args;
+ {
+ 	char salt [OSOFFSET + SALTSIZE]; 	/* Size is system dependant. */
+ 	int cur;			/* The current offset into  salter[]. */
+ 	int i;					/* Iterate don the SALTSIZE. */
+ 
+ 	extern int rand(void);			/* Bad rand num generator. */
+ 	extern void srand(unsigned int seed);	/* Seeder for rand(3). */
+ 	extern time_t time(time_t *t);		/* Helper for the seed. */
+ 
+ 
+ 
+ 	/* Not very good seed, but should be better than most people,
+ 	 * well create. 'Just another sign of the times.' */
+ 	srand (  time(NULL)  );
+ 
+ 	/* This is where the os dependant salt code should go.  
+ 	 * But it's not yet impelented... */
+ 
+ 	for (i = 0; i < SALTSIZE; i++) {
+ 		/* Make sure that the char we pick falls into the array
+ 		 * bounds of salter[]. */
+ 		cur = rand () % sizeof (salter);
+ 		
+ 		/* Fill in the salt with the current chosen char. */
+ 		salt[OSOFFSET + i] = salter[cur];
+ 	}
+ 		
+ 	/* There is no way to error that i can see. */
+ 	return PyString_FromString (salt);
+ }
+ 
+ static char crypt_salt__doc__[] = "\
+ salt() -> string\n\
+ word will be the system default salt value that should be passed\n\
+ to crypt() so that you will get the best encryped password for\n\
+ your system.  This offloads the salt generation into one easily\n\
+ maintained function.";
+ 
+ 
+ /* Call the sys crypt to do the cript, it should accept standard
+  * 2 char salts as well as os depandant salts as create by 
+  * crypt.salt(). */
+ 
+ 
  static PyObject *crypt_crypt(self, args)
  	PyObject *self, *args;
  {
***************
*** 33,38 ****
--- 109,115 ----
  
  static PyMethodDef crypt_methods[] = {
  	{"crypt",	crypt_crypt, 0, crypt_crypt__doc__},
+ 	{"salt",	crypt_salt, 0, crypt_salt__doc__},
  	{NULL,		NULL}		/* sentinel */
  };
  

--7JfCtLOvnd9MIVvH--