(root)/
glibc-2.38/
manual/
examples/
testpass.c
       1  /* Verify a passphrase.
       2     Copyright (C) 1991-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU General Public License
       6     as published by the Free Software Foundation; either version 2
       7     of the License, or (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program; if not, see <https://www.gnu.org/licenses/>.
      16  */
      17  
      18  #include <stdio.h>
      19  #include <string.h>
      20  #include <unistd.h>
      21  #include <crypt.h>
      22  
      23  /* @samp{GNU's Not Unix} hashed using SHA-256, MD5, and DES.  */
      24  static const char hash_sha[] =
      25    "$5$DQ2z5NHf1jNJnChB$kV3ZTR0aUaosujPhLzR84Llo3BsspNSe4/tsp7VoEn6";
      26  static const char hash_md5[] = "$1$A3TxDv41$rtXVTUXl2LkeSV0UU5xxs1";
      27  static const char hash_des[] = "FgkTuF98w5DaI";
      28  
      29  int
      30  main(void)
      31  {
      32    char *phrase;
      33    int status = 0;
      34  
      35    /* Prompt for a passphrase.  */
      36    phrase = getpass ("Enter passphrase: ");
      37  
      38    /* Compare against the stored hashes.  Any input that begins with
      39       @samp{GNU's No} will match the DES hash, but the other two will
      40       only match @samp{GNU's Not Unix}.  */
      41  
      42    if (strcmp (crypt (phrase, hash_sha), hash_sha))
      43      {
      44        puts ("SHA: not ok");
      45        status = 1;
      46      }
      47    else
      48      puts ("SHA: ok");
      49  
      50    if (strcmp (crypt (phrase, hash_md5), hash_md5))
      51      {
      52        puts ("MD5: not ok");
      53        status = 1;
      54      }
      55    else
      56      puts ("MD5: ok");
      57  
      58    if (strcmp (crypt (phrase, hash_des), hash_des))
      59      {
      60        puts ("DES: not ok");
      61        status = 1;
      62      }
      63    else
      64      puts ("DES: ok");
      65  
      66    return status;
      67  }