/* ================================================================================ RPL/2 (R) version 4.1.32 Copyright (C) 1989-2020 Dr. BERTRAND Joël This file is part of RPL/2. RPL/2 is free software; you can redistribute it and/or modify it under the terms of the CeCILL V2 License as published by the french CEA, CNRS and INRIA. RPL/2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL V2 License for more details. You should have received a copy of the CeCILL License along with RPL/2. If not, write to info@cecill.info. ================================================================================ */ #include #include #include #include "openssl/evp.h" #include #include int main(int argc, char *argv[]) { EVP_MD_CTX *contexte; int in_fd; off_t taille_fichier; ssize_t octets_lus; struct stat stat_buf; unsigned char *chaine; unsigned char *fichier; unsigned char somme[EVP_MAX_MD_SIZE]; unsigned char somme_hexadecimale[2 * EVP_MAX_MD_SIZE]; unsigned int i; unsigned int longueur_somme; if (argc != 2) { printf("Usage: rplmd5sum filename\n"); exit(EXIT_FAILURE); } fichier = argv[1]; if (stat(fichier, &stat_buf) != 0) { fprintf(stderr, "stat() failed\n"); perror("stat"); exit(EXIT_FAILURE); } taille_fichier = stat_buf.st_size; if ((chaine = malloc(taille_fichier)) == NULL) { fprintf(stderr, "malloc() failed\n"); exit(EXIT_FAILURE); } if ((in_fd = open(fichier, 0, O_RDONLY)) < 0) { fprintf(stderr, "open() failed\n"); perror("open"); free(chaine); exit(EXIT_FAILURE); } if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier) { # ifndef OS2 // Sur des fichiers binaires et sous OS/2, le nombre d'octets lus peut // être inférieur à la taille du fichier sur le disque. fprintf(stderr, "read() failed\n"); fprintf(stderr, "Meditation: %d < %d\n", (int) octets_lus, (int) taille_fichier); perror("read"); close(in_fd); free(chaine); exit(EXIT_FAILURE); # endif } close(in_fd); if ((contexte = EVP_MD_CTX_new()) == NULL) { fprintf(stderr, "EVP_MD_CTX_new() failed\n"); close(in_fd); free(chaine); exit(EXIT_FAILURE); } if (EVP_DigestInit(contexte, EVP_sum()) != 1) { fprintf(stderr, "EVP_DigestInit() failed\n"); EVP_MD_CTX_free(contexte); close(in_fd); free(chaine); exit(EXIT_FAILURE); } if (EVP_DigestUpdate(contexte, chaine, taille_fichier) != 1) { fprintf(stderr, "EVP_DigestUpdate() failed\n"); EVP_MD_CTX_free(contexte); close(in_fd); free(chaine); exit(EXIT_FAILURE); } if (EVP_DigestFinal_ex(contexte, somme, &longueur_somme) != 1) { fprintf(stderr, "EVP_DigestFinal_ex() failed\n"); EVP_MD_CTX_free(contexte); close(in_fd); free(chaine); exit(EXIT_FAILURE); } EVP_MD_CTX_free(contexte); free(chaine); for(i = 0; i < longueur_somme; i++) { sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]); } printf("%s\n", somme_hexadecimale); return(EXIT_SUCCESS); } // vim: ts=4