#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - pwcrypt8     *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
# Takes the environment variable PW_CLEAR and prints its encrypted version
# to STDOUT. Handles cleartext passwords of max. 8 characters.
#
#****************************************************************************

BEGIN \
{
  NULL = ""
  pwclear = substr( ENVIRON["PW_CLEAR"], 1 ,8 )
  if ( pwclear == NULL ) exit 1

  # Set string for 'salt'.
  S = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/."
  l = length( S )

  # Init. the pseudo-random generator.
  srand()

  # Compute the first 'salt' character.
  s1 = l + 1;   while ( s1 > l ) s1 = int( rand() * 100 )
  s1 = substr( S, s1, 1 )

  # Compute the second 'salt' character.
  s2 = l + 1;   while ( s2 > l ) s2 = int( rand() * 100 )
  s2 = substr( S, s2, 1 )

  salt = s1 s2

  print pwclear "\n" salt | " ${NSQLIB}/bin/crypt8"
}

