ATLAS Offline Software
VP1MD5Sum.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Implementation of class VP1MD5Sum //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: May 2008 //
12 // //
14 
15 #include "VP1Gui/VP1MD5Sum.h"
16 #include <QFileInfo>
17 #include <QCryptographicHash>
18 #include <QRegExp>
19 
20 //____________________________________________________________________
22 public:
23  static bool fileok(const QString& filename);
24  static QByteArray actualMd5Sum( const QString& filename, bool&ok );
25 };
26 
27 //____________________________________________________________________
28 QByteArray VP1MD5Sum::sum(const QString& filename)
29 {
30  if (!Imp::fileok(filename))
31  return QByteArray();
32  bool ok;
33  QByteArray arr = Imp::actualMd5Sum(filename,ok);
34  return ok ? arr : QByteArray();
35 }
36 
37 //____________________________________________________________________
38 bool VP1MD5Sum::sumMatches( const QString& filename, const QByteArray& targetmd5sum )
39 {
40  if (targetmd5sum.isEmpty()||!Imp::fileok(filename))
41  return false;
42  bool ok;
43  return targetmd5sum==Imp::actualMd5Sum(filename,ok) && ok;
44 }
45 
46 //____________________________________________________________________
47 bool VP1MD5Sum::sumMatches( const QString& filename, const QString& md5sum )
48 {
49  return sumMatches( filename, sumToByteArray(md5sum) );
50 }
51 
52 //____________________________________________________________________
53 bool VP1MD5Sum::Imp::fileok(const QString& filename)
54 {
55  QFileInfo fi(filename);
56  return fi.exists() && fi.isReadable();
57 }
58 
59 //____________________________________________________________________
60 QByteArray VP1MD5Sum::Imp::actualMd5Sum( const QString& filename, bool&ok )
61 {
62  ok = false;
63  QFile file(filename);
64  QCryptographicHash md5Hash(QCryptographicHash::Md5);
65 
66  if (file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
67  ok = true;
68  while (!file.atEnd())
69  md5Hash.addData(file.read(10485760));
70  }
71  file.close();
72  return md5Hash.result();
73 }
74 
75 //____________________________________________________________________
76 QString VP1MD5Sum::sumToString(const QByteArray& ba )
77 {
78  return ba.toHex();
79 }
80 
81 //____________________________________________________________________
82 QByteArray VP1MD5Sum::sumToByteArray(const QString& str )
83 {
84  return QByteArray::fromHex(str.toUtf8());
85 }
86 
87 //____________________________________________________________________
88 bool VP1MD5Sum::validMD5Sum(const QString& sum)
89 {
90  if (sum.count()!=32)
91  return false;
92  return QRegExp("[0-9a-z]*").exactMatch(sum.toLower());
93 }
94 
95 //____________________________________________________________________
96 QString VP1MD5Sum::sumToStandardFormat(const QString& sum)
97 {
98  return sum.simplified().toLower();
99 }
VP1MD5Sum::sum
static QByteArray sum(const QString &filename)
Definition: VP1MD5Sum.cxx:28
VP1MD5Sum::Imp::fileok
static bool fileok(const QString &filename)
Definition: VP1MD5Sum.cxx:53
VP1MD5Sum::validMD5Sum
static bool validMD5Sum(const QString &)
Definition: VP1MD5Sum.cxx:88
VP1MD5Sum::Imp::actualMd5Sum
static QByteArray actualMd5Sum(const QString &filename, bool &ok)
Definition: VP1MD5Sum.cxx:60
file
TFile * file
Definition: tile_monitor.h:29
VP1MD5Sum::sumMatches
static bool sumMatches(const QString &filename, const QByteArray &md5sum)
Definition: VP1MD5Sum.cxx:38
VP1MD5Sum::sumToString
static QString sumToString(const QByteArray &)
Definition: VP1MD5Sum.cxx:76
VP1MD5Sum::sumToStandardFormat
static QString sumToStandardFormat(const QString &)
Definition: VP1MD5Sum.cxx:96
VP1MD5Sum::sumToByteArray
static QByteArray sumToByteArray(const QString &)
Definition: VP1MD5Sum.cxx:82
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
checkFileSG.fi
fi
Definition: checkFileSG.py:65
VP1MD5Sum.h
str
Definition: BTagTrackIpAccessor.cxx:11
VP1MD5Sum::Imp
Definition: VP1MD5Sum.cxx:21