#include <base/io/FileInputStream.h>
#include <base/io/FileReader.h>
#include <base/filesystem/FileSystem.h>
#include <base/filesystem/FolderInfo.h>
#include <base/string/FormatOutputStream.h>
#include <base/Application.h>
#include <base/security/Adler32.h>
#include <base/security/CRC32.h>
#include <base/security/MD5Sum.h>
#include <base/security/SHA1.h>
#include <base/security/SHA256.h>
#include <base/security/SHA384.h>
#include <base/security/SHA512.h>
using namespace com::azure::dev::base;
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 0;
static const unsigned int BLOCK_SIZE = 4096 * 4;
uint8 buffer[BLOCK_SIZE];
unsigned int job = 0;
public:
class Job {
public:
enum {
ADLER32,
MD5SUM,
};
};
DigestApplication()
{
job = Job::SHA1;
}
void jobAdler32(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << HEX << setWidth(8) << NOPREFIX << ZEROPAD << sum.
getValue()
<< indent(2) << path << ENDL;
}
void jobCRC32(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << HEX << setWidth(8) << NOPREFIX << ZEROPAD << sum.
getValue()
<< indent(2) << path << ENDL;
}
void jobMD5Sum(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << sum.
getValue() << indent(2) << path << ENDL;
}
void jobSHA1(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << sum.
getValue() << indent(2) << path << ENDL;
}
void jobSHA256(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << sum.
getValue() << indent(2) << path << ENDL;
}
void jobSHA384(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << sum.
getValue() << indent(2) << path << ENDL;
}
void jobSHA512(
const String& path) {
unsigned int count = 0;
while ((count = file.
read(buffer,
sizeof(buffer),
true)) > 0) {
}
fout << sum.
getValue() << indent(2) << path << ENDL;
}
void fileDigest(
const String& path) {
switch (job) {
case Job::ADLER32:
jobAdler32(path);
break;
case Job::CRC32:
jobCRC32(path);
break;
case Job::MD5SUM:
jobMD5Sum(path);
break;
case Job::SHA1:
jobSHA1(path);
break;
case Job::SHA256:
jobSHA256(path);
break;
case Job::SHA384:
jobSHA384(path);
break;
case Job::SHA512:
jobSHA512(path);
break;
}
}
void digest(
const String& path)
{
unsigned int type = 0;
try {
ferr << "Error: " << "File or folder does not exist" << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
fileDigest(path);
while (enu.hasNext()) {
const String entry = enu.next();
try {
fileDigest(entry);
}
}
}
} else {
ferr << "Error: " << "Invalid filesystem entry" << ENDL;
setExitCode(EXIT_CODE_ERROR);
}
}
void usage()
{
fout << "Usage: " << getFormalName()
<< " [ADLER32|CRC32|MD5SUM|SHA1|SHA256|SHA384|SHA512] file" << ENDL;
}
void main()
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
case 2:
jobString = arguments[0];
if (jobString == "ADLER32") {
job = Job::ADLER32;
} else if (jobString == "CRC32") {
job = Job::CRC32;
} else if (jobString == "MD5SUM") {
job = Job::MD5SUM;
} else if (jobString == "SHA1") {
job = Job::SHA1;
} else if (jobString == "SHA256") {
job = Job::SHA256;
} else if (jobString == "SHA384") {
job = Job::SHA384;
} else if (jobString == "SHA512") {
job = Job::SHA512;
} else {
usage();
setExitCode(EXIT_CODE_ERROR);
return;
}
path = arguments[1];
break;
default:
usage();
}
try {
digest(path);
}
}
};
APPLICATION_STUB(DigestApplication);