
#use wml::openssl-macros area=docs page=EVP_DigestInit

<title>Documents, EVP_DigestInit(3)</title>

<h1>EVP_DigestInit(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#EXAMPLE">EXAMPLE</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
EVP_MD_CTX_init, EVP_MD_CTX_create, EVP_DigestInit_ex, EVP_DigestUpdate,
EVP_DigestFinal_ex, EVP_MD_CTX_cleanup, EVP_MD_CTX_destroy,
EVP_MAX_MD_SIZE, EVP_MD_CTX_copy_ex, EVP_MD_CTX_copy, EVP_MD_type,
EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_CTX_md,
EVP_MD_CTX_size, EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_md_null,
EVP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha224, EVP_sha256, EVP_sha384,
EVP_sha512, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160,
EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj - EVP digest
routines

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/evp.h&gt;
</PRE>
<PRE> void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
 EVP_MD_CTX *EVP_MD_CTX_create(void);
</PRE>
<PRE> int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md,
        unsigned int *s);
</PRE>
<PRE> int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
 void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
</PRE>
<PRE> int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);  
</PRE>
<PRE> int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
        unsigned int *s);
</PRE>
<PRE> int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);  
</PRE>
<PRE> #define EVP_MAX_MD_SIZE 64     /* SHA512 */
</PRE>
<PRE> int EVP_MD_type(const EVP_MD *md);
 int EVP_MD_pkey_type(const EVP_MD *md);        
 int EVP_MD_size(const EVP_MD *md);
 int EVP_MD_block_size(const EVP_MD *md);
</PRE>
<PRE> const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
 \#define EVP_MD_CTX_size(e)             EVP_MD_size(EVP_MD_CTX_md(e))
 \#define EVP_MD_CTX_block_size(e)       EVP_MD_block_size((e)-&gt;digest)
 \#define EVP_MD_CTX_type(e)             EVP_MD_type((e)-&gt;digest)
</PRE>
<PRE> const EVP_MD *EVP_md_null(void);
 const EVP_MD *EVP_md2(void);
 const EVP_MD *EVP_md5(void);
 const EVP_MD *EVP_sha(void);
 const EVP_MD *EVP_sha1(void);
 const EVP_MD *EVP_dss(void);
 const EVP_MD *EVP_dss1(void);
 const EVP_MD *EVP_mdc2(void);
 const EVP_MD *EVP_ripemd160(void);
</PRE>
<PRE> const EVP_MD *EVP_sha224(void);
 const EVP_MD *EVP_sha256(void);
 const EVP_MD *EVP_sha384(void);
 const EVP_MD *EVP_sha512(void);
</PRE>
<PRE> const EVP_MD *EVP_get_digestbyname(const char *name);
 \#define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
 \#define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
The EVP digest routines are a high level interface to message digests.

</P>
<P>
<CODE>EVP_MD_CTX_init()</CODE> initializes digest context <STRONG>ctx</STRONG>.

</P>
<P>
<CODE>EVP_MD_CTX_create()</CODE> allocates, initializes and returns a
digest context.

</P>
<P>
<CODE>EVP_DigestInit_ex()</CODE> sets up digest context <STRONG>ctx</STRONG> to use a digest
<STRONG>type</STRONG> from ENGINE <STRONG>impl</STRONG>. <STRONG>ctx</STRONG> must be initialized before calling this function. <STRONG>type</STRONG> will typically be supplied by a functionsuch as <CODE>EVP_sha1().</CODE> If <STRONG>impl</STRONG> is NULL then the default implementation of digest <STRONG>type</STRONG> is used.

</P>
<P>
<CODE>EVP_DigestUpdate()</CODE> hashes <STRONG>cnt</STRONG> bytes of data at <STRONG>d</STRONG> into the digest context <STRONG>ctx</STRONG>. This function can be called several times on the same <STRONG>ctx</STRONG> to hash additional data.

</P>
<P>
<CODE>EVP_DigestFinal_ex()</CODE> retrieves the digest value from <STRONG>ctx</STRONG> and places it in <STRONG>md</STRONG>. If the <STRONG>s</STRONG> parameter is not NULL then the number of bytes of data written (i.e. the
length of the digest) will be written to the integer at <STRONG>s</STRONG>, at most <STRONG>EVP_MAX_MD_SIZE</STRONG> bytes will be written. After calling <CODE>EVP_DigestFinal_ex()</CODE> no
additional calls to <CODE>EVP_DigestUpdate()</CODE> can be made, but
<CODE>EVP_DigestInit_ex()</CODE> can be called to initialize a new digest
operation.

</P>
<P>
<CODE>EVP_MD_CTX_cleanup()</CODE> cleans up digest context <STRONG>ctx</STRONG>, it should be called after a digest context is no longer needed.

</P>
<P>
<CODE>EVP_MD_CTX_destroy()</CODE> cleans up digest context <STRONG>ctx</STRONG> and frees up the space allocated to it, it should be called only on a
context created using <CODE>EVP_MD_CTX_create().</CODE>

</P>
<P>
<CODE>EVP_MD_CTX_copy_ex()</CODE> can be used to copy the message digest
state from
<STRONG>in</STRONG> to <STRONG>out</STRONG>. This is useful if large amounts of data are to be hashed which only
differ in the last few bytes. <STRONG>out</STRONG> must be initialized before calling this function.

</P>
<P>
<CODE>EVP_DigestInit()</CODE> behaves in the same way as
<CODE>EVP_DigestInit_ex()</CODE> except the passed context <STRONG>ctx</STRONG> does not have to be initialized, and it always uses the default digest
implementation.

</P>
<P>
<CODE>EVP_DigestFinal()</CODE> is similar to
<CODE>EVP_DigestFinal_ex()</CODE> except the digest context <STRONG>ctx</STRONG> is automatically cleaned up.

</P>
<P>
<CODE>EVP_MD_CTX_copy()</CODE> is similar to
<CODE>EVP_MD_CTX_copy_ex()</CODE> except the destination
<STRONG>out</STRONG> does not have to be initialized.

</P>
<P>
<CODE>EVP_MD_size()</CODE> and <CODE>EVP_MD_CTX_size()</CODE> return the
size of the message digest when passed an <STRONG>EVP_MD</STRONG> or an <STRONG>EVP_MD_CTX</STRONG> structure, i.e. the size of the hash.

</P>
<P>
<CODE>EVP_MD_block_size()</CODE> and <CODE>EVP_MD_CTX_block_size()</CODE>
return the block size of the message digest when passed an <STRONG>EVP_MD</STRONG> or an <STRONG>EVP_MD_CTX</STRONG> structure.

</P>
<P>
<CODE>EVP_MD_type()</CODE> and <CODE>EVP_MD_CTX_type()</CODE> return the
NID of the OBJECT IDENTIFIER representing the given message digest when
passed an <STRONG>EVP_MD</STRONG> structure. For example <CODE>EVP_MD_type(EVP_sha1())</CODE> returns <STRONG>NID_sha1</STRONG>. This function is normally used when setting ASN1 OIDs.

</P>
<P>
<CODE>EVP_MD_CTX_md()</CODE> returns the <STRONG>EVP_MD</STRONG> structure corresponding to the passed
<STRONG>EVP_MD_CTX</STRONG>.

</P>
<P>
<CODE>EVP_MD_pkey_type()</CODE> returns the NID of the public key signing
algorithm associated with this digest. For example <CODE>EVP_sha1()</CODE>
is associated with RSA so this will return <STRONG>NID_sha1WithRSAEncryption</STRONG>. Since digests and signature algorithms are no longer linked this function
is only retained for compatibility reasons.

</P>
<P>
<CODE>EVP_md2(),</CODE> <CODE>EVP_md5(),</CODE> <CODE>EVP_sha(),</CODE>
<CODE>EVP_sha1(),</CODE> <CODE>EVP_sha224(),</CODE>
<CODE>EVP_sha256(),</CODE> <CODE>EVP_sha384(),</CODE>
<CODE>EVP_sha512(),</CODE> <CODE>EVP_mdc2()</CODE> and
<CODE>EVP_ripemd160()</CODE> return <STRONG>EVP_MD</STRONG>
structures for the MD2, MD5, SHA, SHA1, SHA224, SHA256, SHA384, SHA512,
MDC2 and RIPEMD160 digest algorithms respectively. 

</P>
<P>
<CODE>EVP_dss()</CODE> and <CODE>EVP_dss1()</CODE> return <STRONG>EVP_MD</STRONG> structures for SHA and SHA1 digest algorithms but using DSS (DSA) for the
signature algorithm. Note: there is no need to use these pseudo-digests in
OpenSSL 1.0.0 and later, they are however retained for compatibility.

</P>
<P>
<CODE>EVP_md_null()</CODE> is a ``null'' message digest that does nothing:
i.e. the hash it returns is of zero length.

</P>
<P>
<CODE>EVP_get_digestbyname(),</CODE> <CODE>EVP_get_digestbynid()</CODE> and
<CODE>EVP_get_digestbyobj()</CODE> return an <STRONG>EVP_MD</STRONG> structure when passed a digest name, a digest NID or an ASN1_OBJECT
structure respectively. The digest table must be initialized using, for
example, <CODE>OpenSSL_add_all_digests()</CODE> for these functions to
work.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>EVP_DigestInit_ex(),</CODE> <CODE>EVP_DigestUpdate()</CODE> and
<CODE>EVP_DigestFinal_ex()</CODE> return 1 for success and 0 for failure.

</P>
<P>
<CODE>EVP_MD_CTX_copy_ex()</CODE> returns 1 if successful or 0 for failure.

</P>
<P>
<CODE>EVP_MD_type(),</CODE> <CODE>EVP_MD_pkey_type()</CODE> and
<CODE>EVP_MD_type()</CODE> return the NID of the corresponding OBJECT
IDENTIFIER or NID_undef if none exists.

</P>
<P>
<CODE>EVP_MD_size(),</CODE> <CODE>EVP_MD_block_size(),</CODE>
<CODE>EVP_MD_CTX_size(e),</CODE> <CODE>EVP_MD_size(),</CODE>
<CODE>EVP_MD_CTX_block_size()</CODE> and <CODE>EVP_MD_block_size()</CODE>
return the digest or block size in bytes.

</P>
<P>
<CODE>EVP_md_null(),</CODE> <CODE>EVP_md2(),</CODE> <CODE>EVP_md5(),</CODE>
<CODE>EVP_sha(),</CODE> <CODE>EVP_sha1(),</CODE> <CODE>EVP_dss(),</CODE>
<CODE>EVP_dss1(),</CODE> <CODE>EVP_mdc2()</CODE> and
<CODE>EVP_ripemd160()</CODE> return pointers to the corresponding EVP_MD
structures.

</P>
<P>
<CODE>EVP_get_digestbyname(),</CODE> <CODE>EVP_get_digestbynid()</CODE> and
<CODE>EVP_get_digestbyobj()</CODE> return either an <STRONG>EVP_MD</STRONG> structure or NULL if an error occurs.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
The <STRONG>EVP</STRONG> interface to message digests should almost always be used in preference to
the low level interfaces. This is because the code then becomes transparent
to the digest used and much more flexible.

</P>
<P>
New applications should use the SHA2 digest algorithms such as SHA256. The
other digest algorithms are still in common use.

</P>
<P>
For most applications the <STRONG>impl</STRONG> parameter to <CODE>EVP_DigestInit_ex()</CODE> will be set to NULL to use
the default digest implementation.

</P>
<P>
The functions <CODE>EVP_DigestInit(),</CODE> <CODE>EVP_DigestFinal()</CODE>
and <CODE>EVP_MD_CTX_copy()</CODE> are obsolete but are retained to
maintain compatibility with existing code. New applications should use
<CODE>EVP_DigestInit_ex(),</CODE> <CODE>EVP_DigestFinal_ex()</CODE> and
<CODE>EVP_MD_CTX_copy_ex()</CODE> because they can efficiently reuse a
digest context instead of initializing and cleaning it up on each call and
allow non default implementations of digests to be specified.

</P>
<P>
In OpenSSL 0.9.7 and later if digest contexts are not cleaned up after use
memory leaks will occur. 

</P>
<P>
Stack allocation of EVP_MD_CTX structures is common, for example:

</P>
<PRE> EVP_MD_CTX mctx;
 EVP_MD_CTX_init(&amp;mctx);
</PRE>
<P>
This will cause binary compatibility issues if the size of EVP_MD_CTX
structure changes (this will only happen with a major release of OpenSSL).
Applications wishing to avoid this should use
<CODE>EVP_MD_CTX_create()</CODE> instead:

</P>
<PRE> EVP_MD_CTX *mctx;
 mctx = EVP_MD_CTX_create();
</PRE>
<P>
<HR>
<H1><A NAME="EXAMPLE">EXAMPLE</A></H1>
<P>
This example digests the data ``Test Message\n'' and ``Hello World\n'',
using the digest name passed on the command line.

</P>
<PRE> #include &lt;stdio.h&gt;
 \#include &lt;openssl/evp.h&gt;
</PRE>
<PRE> main(int argc, char *argv[])
 {
 EVP_MD_CTX *mdctx;
 const EVP_MD *md;
 char mess1[] = &quot;Test Message\n&quot;;
 char mess2[] = &quot;Hello World\n&quot;;
 unsigned char md_value[EVP_MAX_MD_SIZE];
 int md_len, i;
</PRE>
<PRE> OpenSSL_add_all_digests();
</PRE>
<PRE> if(!argv[1]) {
        printf(&quot;Usage: mdtest digestname\n&quot;);
        exit(1);
 }
</PRE>
<PRE> md = EVP_get_digestbyname(argv[1]);
</PRE>
<PRE> if(!md) {
        printf(&quot;Unknown message digest %s\n&quot;, argv[1]);
        exit(1);
 }
</PRE>
<PRE> mdctx = EVP_MD_CTX_create();
 EVP_DigestInit_ex(mdctx, md, NULL);
 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
 EVP_DigestFinal_ex(mdctx, md_value, &amp;md_len);
 EVP_MD_CTX_destroy(mdctx);
</PRE>
<PRE> printf(&quot;Digest is: &quot;);
 for(i = 0; i &lt; md_len; i++) printf(&quot;%02x&quot;, md_value[i]);
 printf(&quot;\n&quot;);
 }
</PRE>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/evp.html#">evp(3)</A>, <A HREF="../crypto/hmac.html#">hmac(3)</A>, <A HREF="../crypto/md5.html#">md2(3)</A>,
<A HREF="../crypto/md5.html#">md5(3)</A>, <A HREF="../crypto/mdc2.html#">mdc2(3)</A>, <A HREF="../crypto/ripemd.html#">ripemd(3)</A>,
<A HREF="../crypto/sha.html#">sha(3)</A>, <A HREF="../apps/dgst.html#">dgst(1)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>EVP_DigestInit(),</CODE> <CODE>EVP_DigestUpdate()</CODE> and
<CODE>EVP_DigestFinal()</CODE> are available in all versions of SSLeay and
OpenSSL.

</P>
<P>
<CODE>EVP_MD_CTX_init(),</CODE> <CODE>EVP_MD_CTX_create(),</CODE>
<CODE>EVP_MD_CTX_copy_ex(),</CODE> <CODE>EVP_MD_CTX_cleanup(),</CODE>
<CODE>EVP_MD_CTX_destroy(),</CODE> <CODE>EVP_DigestInit_ex()</CODE> and
<CODE>EVP_DigestFinal_ex()</CODE> were added in OpenSSL 0.9.7.

</P>
<P>
<CODE>EVP_md_null(),</CODE> <CODE>EVP_md2(),</CODE> <CODE>EVP_md5(),</CODE>
<CODE>EVP_sha(),</CODE> <CODE>EVP_sha1(),</CODE> <CODE>EVP_dss(),</CODE>
<CODE>EVP_dss1(),</CODE> <CODE>EVP_mdc2()</CODE> and
<CODE>EVP_ripemd160()</CODE> were changed to return truely const EVP_MD *
in OpenSSL 0.9.7.

</P>
<P>
The link between digests and signing algorithms was fixed in OpenSSL 1.0
and later, so now <CODE>EVP_sha1()</CODE> can be used with RSA and DSA,
there is no need to use <CODE>EVP_dss1()</CODE> any more.

</P>
<P>
OpenSSL 1.0 and later does not include the MD2 digest algorithm in the
default configuration due to its security weaknesses.

</P>
:}


