bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ost::Digest class Re: protected initDigest() method


From: etb
Subject: Re: ost::Digest class Re: protected initDigest() method
Date: 27 Nov 2003 16:11:11 -0600

address@hidden writes:

> The ost::MD5Digest/ost::SHA1Digest/ost::SHA256Digest classes all
> inherit from the abstract ost::Digest class. The ost::Digest class
> lists a protected abstract method, initDigest(). The digest classes
> that inherit from ost::Digest make their initDigest() methods
> public.

> I want to use the ost::Digest class as an interface to multiple
> digests.  That way I can have a function that takes a ost::Digest*
> and uses it for it's digest computing needs.  The shadigest.cpp demo
> program shows ost::SHA256Digest::initDigest() being called before
> the SHA256Digest instance is used.

> Are newly created MD5/SHA1/SHA256Digests already inited, or does one
> have to call initDigest() before using one?  Why is
> ost::Digest::initDigest() protected and not public?

For the SHA hashes, initDigest() needs to be called prior to any new
hash after the initial one. Since the demo program goes through each
file and we don't want to create a new instance of the digest object,
we call initDigest(). But, initDigest() is called in the constructor
so if you create a new object each time you want a new hash then you
won't need to call it. We could have done this (from shadigest.cpp):

  for(int i = 1 ; i < argc; i++) {
    FILE * fp = fopen(argv[i], "rb");
    if(i > 1) // new
      d256.initDigest();

    if(fp) {
      while((length =  fread(buff, sizeof(unsigned char), BUFF_SIZE, fp))) {
        d256.putDigest(buff, length);
      }
                        
      std::cout << d256 << "  " << argv[i] << std::endl;

      fclose(fp);
    }
  }

As to why initDigest() is protected in the base class, however, I do
not know.

Elizabeth




reply via email to

[Prev in Thread] Current Thread [Next in Thread]