Blame manual/examples/testpass.c

Packit 6c4009
/* Verify a passphrase.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <crypt.h>
Packit 6c4009
Packit 6c4009
/* @samp{GNU's Not Unix} hashed using SHA-256, MD5, and DES.  */
Packit 6c4009
static const char hash_sha[] =
Packit 6c4009
  "$5$DQ2z5NHf1jNJnChB$kV3ZTR0aUaosujPhLzR84Llo3BsspNSe4/tsp7VoEn6";
Packit 6c4009
static const char hash_md5[] = "$1$A3TxDv41$rtXVTUXl2LkeSV0UU5xxs1";
Packit 6c4009
static const char hash_des[] = "FgkTuF98w5DaI";
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main(void)
Packit 6c4009
{
Packit 6c4009
  char *phrase;
Packit 6c4009
  int status = 0;
Packit 6c4009
Packit 6c4009
  /* Prompt for a passphrase.  */
Packit 6c4009
  phrase = getpass ("Enter passphrase: ");
Packit 6c4009
Packit 6c4009
  /* Compare against the stored hashes.  Any input that begins with
Packit 6c4009
     @samp{GNU's No} will match the DES hash, but the other two will
Packit 6c4009
     only match @samp{GNU's Not Unix}.  */
Packit 6c4009
Packit 6c4009
  if (strcmp (crypt (phrase, hash_sha), hash_sha))
Packit 6c4009
    {
Packit 6c4009
      puts ("SHA: not ok");
Packit 6c4009
      status = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    puts ("SHA: ok");
Packit 6c4009
Packit 6c4009
  if (strcmp (crypt (phrase, hash_md5), hash_md5))
Packit 6c4009
    {
Packit 6c4009
      puts ("MD5: not ok");
Packit 6c4009
      status = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    puts ("MD5: ok");
Packit 6c4009
Packit 6c4009
  if (strcmp (crypt (phrase, hash_des), hash_des))
Packit 6c4009
    {
Packit 6c4009
      puts ("DES: not ok");
Packit 6c4009
      status = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    puts ("DES: ok");
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}