X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=blobdiff_plain;f=sha1.cc;h=7111f94ab40ad67fcb7745f90def6861728f0efe;hp=7a9c0fb52a377f27b5a747b9eb1223902ba645b9;hb=f38dd9bcb0caffd3fc9126b05788c936690e8288;hpb=25b6639fb1783e0061affa177e6d6d2131c457f5 diff --git a/sha1.cc b/sha1.cc index 7a9c0fb..7111f94 100644 --- a/sha1.cc +++ b/sha1.cc @@ -1,38 +1,43 @@ /* sha1.cc - Functions to compute SHA1 message digest of data streams - according to the NIST specification FIPS-180-1. - - Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. - Copyright (C) 2006 Michael Vrable - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2, or (at your option) any - later version. - - This program 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 - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -/* Written by Scott G. Miller - Credits: - Robert Klep -- Expansion function fix - - Modified by Michael Vrable to simplify the interface - and add an object-oriented wrapper. Original code (in C) taken from GNU - coreutils (Debian package 5.97-5). -*/ + * according to the NIST specification FIPS-180-1. + * part of Cumulus: Smart Filesystem Backup to Dumb Servers + * + * Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. + * Copyright (C) 2006-2007 The Regents of the University of California + * Written by Scott G. Miller + * Additional Credits: + * Robert Klep -- Expansion function fix + * Modifications by Michael Vrable to integrate into + * Cumulus. + * + * Original code (in C) is taken from GNU coreutils (Debian package 5.97-5). + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ #include "sha1.h" #include +#include #include #include +#include + +using std::string; + /* SWAP does an endian swap on architectures that are little-endian, as SHA1 needs some data in a big-endian form. */ #define SWAP(n) htonl(n) @@ -344,8 +349,46 @@ void SHA1Checksum::process(const void *data, size_t len) sha1_process_bytes(data, len, &ctx); } +bool SHA1Checksum::process_file(const char *filename) +{ + FILE *f = fopen(filename, "rb"); + if (f == NULL) + return false; + + while (!feof(f)) { + char buf[4096]; + size_t bytes = fread(buf, 1, sizeof(buf), f); + + if (ferror(f)) { + fclose(f); + return false; + } + + process(buf, bytes); + } + + fclose(f); + return true; +} + const uint8_t *SHA1Checksum::checksum() { sha1_finish_ctx(&ctx, resbuf); return (const uint8_t *)resbuf; } + +string SHA1Checksum::checksum_str() +{ + uint8_t resbuf[20]; + char hexbuf[4]; + string result = "sha1="; + + sha1_finish_ctx(&ctx, resbuf); + + for (int i = 0; i < 20; i++) { + sprintf(hexbuf, "%02x", resbuf[i]); + result += hexbuf; + } + + return result; +}