<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>askyb.com</title>
	<atom:link href="http://www.askyb.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.askyb.com</link>
	<description>Programming Tips and Tuorials</description>
	<lastBuildDate>Wed, 12 Jun 2013 08:07:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Changing Screen Resolution in Ubuntu Server</title>
		<link>http://www.askyb.com/ubuntu/changing-screen-resolution-in-ubuntu-server/</link>
		<comments>http://www.askyb.com/ubuntu/changing-screen-resolution-in-ubuntu-server/#comments</comments>
		<pubDate>Wed, 12 Jun 2013 07:28:42 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[vga]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2764</guid>
		<description><![CDATA[Facing an issue with how to change the screen resolution for your newly setup Ubuntu server box? Here is the simple guide on how to change the screen resolution. The following table lists some of the available VGA codes for changing of screen resolution purposes. VGA Resolution Codes for GRUB &#38; Lilo Depth 640&#215;480 800×600...]]></description>
				<content:encoded><![CDATA[<p>Facing an issue with how to change the screen resolution for your newly setup Ubuntu server box? Here is the simple guide on how to change the screen resolution.</p>
<span id="more-2764"></span>
<p>The following table lists some of the available VGA codes for changing of screen resolution purposes.</p>
<p>
<u><i>VGA Resolution Codes for GRUB &amp; Lilo</i></u>

<table border="0">
<tbody>
<tr>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">Depth</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">640&#215;480</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">800×600</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1024×768</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1152×864</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1280&#215;800</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1280×1024</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1440&#215;900</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">1600×1200</td>
</tr>
<tr style="border: 1px solid black;">
<td style="border-color: #000000; border-style: solid; border-width: 1px;">8</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">769</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">771</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">773</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">353</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">864</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">775</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">868</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">796</td>
</tr>
<tr style="border: 1px solid black;">
<td style="border-color: #000000; border-style: solid; border-width: 1px;">16</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">785</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">788</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">791</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">355</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">???</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">794</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">???</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">798</td>
</tr>
</tr>
<tr style="border: 1px solid black;">
<td style="border-color: #000000; border-style: solid; border-width: 1px;">32</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">786</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">789</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">792</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">???</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">865</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">???</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">869</td>
<td style="border-color: #000000; border-style: solid; border-width: 1px;">???</td>
</tr>
</tbody></table>
</p>

<p>
To change the screen resolution, let&#8217;s start by updating the grub file. <br />

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>default<span style="color: #000000; font-weight: bold;">/</span>grub</div></div>

or if you prefer vi then <br />

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>default<span style="color: #000000; font-weight: bold;">/</span>grub</div></div>

</p>

<p>
Look for the line of <b>CRUB_CMDLINE_LINUX=</b>. To set the screen resolution to 800&#215;600 for 8 bits of color depth, we will need to enter vga code 771 as the following.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">CRUB_CMDLINE_LINUX</span>=<span style="color: #ff0000;">&quot;vga=771&quot;</span></div></div>

</p>

<p>
Save the changes to the file. Then, run the following command to update the grub.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> update-grub</div></div>

</p>

<p>
Restart the system now and you should observe that the screen resolution has changed.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> shutdown <span style="color: #660033;">-r</span> now</div></div>

</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/ubuntu/changing-screen-resolution-in-ubuntu-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load JSON File Locally by Javascript Without JQuery</title>
		<link>http://www.askyb.com/javascript/load-json-file-locally-by-js-without-jquery/</link>
		<comments>http://www.askyb.com/javascript/load-json-file-locally-by-js-without-jquery/#comments</comments>
		<pubDate>Tue, 11 Jun 2013 08:42:12 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2746</guid>
		<description><![CDATA[Loading of JSON file by javascript is simple. Moreover, it can be done without using of JQuery at all. This tutorial will show you how to accomplish the mission easily. Let&#8217;s start by creating a JSON file with the following content. Save the filename as &#8220;data.json&#8221;. [crayon-51c23d098d596/] Let&#8217;s create a javascript file to read the...]]></description>
				<content:encoded><![CDATA[<p> Loading of JSON file by javascript is simple. Moreover, it can be done without using of JQuery at all. This tutorial will show you how to accomplish the mission easily.<p>
<span id="more-2746"></span>

<p>Let&#8217;s start by creating a JSON file with the following content. Save the filename as <b>&#8220;data.json&#8221;.</a>
 
</p><pre class="crayon-plain-tag">data = '[{"name" : "Harry", "age" : "32"}]';</pre><p> 

</p>

<p>Let&#8217;s create a javascript file to read the json data as following. Save the javascript file as <b>&#8220;myscript.js&#8221;.</a>
 
</p><pre class="crayon-plain-tag">function load() {
	var mydata = JSON.parse(data);
	alert(mydata[0].name);
	alert(mydata[0].age);
}</pre><p> 

</p>

<p>We are yet to complete the tutorial. Still we need to load both files in the html file that we are going to create now. Create a HTML file as following:
 
</p><pre class="crayon-plain-tag">&lt;!doctype html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;askyb - Load JSON File Locally by Javascript Without JQuery&lt;/title&gt;
        &lt;script type="text/javascript" src="file.json"&gt;&lt;/script&gt;
        &lt;script type="text/javascript" src="myscript.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body onload="load()"&gt;
        askyb - Load JSON File Locally by Javascript Without JQuery
    &lt;/body&gt;
&lt;/html&gt;</pre><p> 

Save the HTML file as <b>&#8220;test.html&#8221;.</a>
<p/>

<p>
Run the HTML file in your browser and you will observe the following output:
<img class="aligncenter size-full" src="http://www.askyb.com/wp-content/uploads/2013/06/json_js.png" alt="Load JSON File Locally by Javascript Without JQuery" />

</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/javascript/load-json-file-locally-by-js-without-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get System Locale in Visual C++</title>
		<link>http://www.askyb.com/cpp/get-system-locale-in-visual-cpp/</link>
		<comments>http://www.askyb.com/cpp/get-system-locale-in-visual-cpp/#comments</comments>
		<pubDate>Wed, 29 May 2013 14:35:11 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[locale]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2727</guid>
		<description><![CDATA[This tutorial will guide your on how to obtain the Windows system locale using of Visual C++. The locale output will be presented in ISO format: [language]_[territory] For example, United States English will be presented as en-US. United Kingdom will be presented as en_GB. Germany german will be presented as de_DE, etc Let&#8217;s create a...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide your on how to obtain the Windows system locale using of Visual C++. The locale output will be presented in ISO format: </p>
<span id="more-2727"></span>
<p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[language]_[territory]</div></div>

For example, United States English will be presented as <b>en-US</b>. United Kingdom will be presented as <b>en_GB</b>. Germany german will be presented as <b>de_DE</b>, etc
</p>

<p>
Let&#8217;s create a C++ Win32 project using Visual C++ 2010 Expresss. Enter the following code to the main().
</p>

<p> 
</p><pre class="crayon-plain-tag">#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;Windows.h&gt;

int _tmain(int argc, _TCHAR* argv[])
{
	WCHAR szISOLang[5] = {0};
	WCHAR szISOCountry[5] = {0};
	int iLen = 0;

	::GetLocaleInfo(LOCALE_USER_DEFAULT,
					LOCALE_SISO639LANGNAME,
					szISOLang,
					sizeof(szISOLang) / sizeof(WCHAR));

	::GetLocaleInfo(LOCALE_USER_DEFAULT,
					LOCALE_SISO3166CTRYNAME,
					szISOCountry,
					sizeof(szISOCountry) / sizeof(WCHAR));

	std::wcout &lt;&lt; szISOLang &lt;&lt; "_" &lt;&lt; szISOCountry &lt;&lt; std::endl;

	return 0;
}</pre><p> 
</p>

<p>Compile and run the project. Try to change the regional and language setting of your system. You may observe the output as following:</p>

<img class="aligncenter size-full" src="http://www.askyb.com/wp-content/uploads/2013/05/locale.png" alt="Get System Locale in Visual C++" />
 ]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/get-system-locale-in-visual-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Socket Example: TCP Client and Server</title>
		<link>http://www.askyb.com/windows-socket/windows-socket-example-tcp-client-and-server/</link>
		<comments>http://www.askyb.com/windows-socket/windows-socket-example-tcp-client-and-server/#comments</comments>
		<pubDate>Fri, 01 Mar 2013 08:33:55 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[Windows Socket]]></category>
		<category><![CDATA[tcp]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2687</guid>
		<description><![CDATA[This tutorial will guide you on how to create a simple tcp/ip client and server application by using windows socket 2 (Winsock2). The following diagram illustrate the design of the application that we&#8217;re going to create in this tutorial. The server will listen at a given IP &#038; port number. whenever it receive message from...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to create a simple tcp/ip client and server application by using windows socket 2 (Winsock2). The following diagram illustrate the design of the application that we&#8217;re going to create in this tutorial.</p>
<span id="more-2687"></span>
<p>The server will listen at a given IP &#038; port number. whenever it receive message from its connected client, it then print out the message and bouce back the message to the client. On the other hand, the client will connect to the server and user will enter a message to send to the server. the client will print out the message that it receive from the server too. To stop the connection, the client will need to send a message <strong>&#8220;close&#8221;</strong> to the server.</p>
<p><img class="aligncenter size-full" src="http://www.askyb.com/wp-content/uploads/2013/03/simple-tcp-client.png" alt="Windows Socket Example" /></p>

<p>
<ol>
<li>Create a new visual C++ empty project using visual studio 2010 express edition. Name the project <strong>ClientExample</strong>.</li>
<li>Add a new visual C++ empty project to the same solution. Name it <strong>ServerExample</strong>.</li>
<li>Add a main.cpp file to both ClientExample &#038; ServerExample project.</li>
<img class="aligncenter size-full" src="http://www.askyb.com/wp-content/uploads/2013/03/projects.png" alt="Windows Socket Example" />
<li>Enter the following code to main.cpp in the <b>ClientExample</b> project.</li>
 
</p><pre class="crayon-plain-tag">#define WIN32_LEAN_AND_MEAN

#include &lt;WinSock2.h&gt;
#include &lt;WS2tcpip.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

// link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_PORT "27015" 
#define DEFAULT_BUFFER_LENGTH	512

class Client {
public:
	Client(char* servername)
	{
		szServerName = servername;
		ConnectSocket = INVALID_SOCKET;
	}

	bool Start() {
		WSADATA wsaData;

		// Initialize Winsock
		int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
		if(iResult != 0)
		{
			printf("WSAStartup failed: %d\n", iResult);
			return false;
		}

		struct addrinfo	*result = NULL,
						*ptr = NULL,
						hints;

		ZeroMemory(&amp;hints, sizeof(hints));
		hints.ai_family = AF_UNSPEC;		
		hints.ai_socktype = SOCK_STREAM;	
		hints.ai_protocol = IPPROTO_TCP;

		// Resolve the server address and port
		iResult = getaddrinfo(szServerName, DEFAULT_PORT, &amp;hints, &amp;result);
		if (iResult != 0)
		{
			printf("getaddrinfo failed: %d\n", iResult);
			WSACleanup();
			return false;
		}

		ptr = result;

		// Create a SOCKET for connecting to server
		ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Error at socket(): %d\n", WSAGetLastError());
			freeaddrinfo(result);
			WSACleanup();
			return false;
		}

		// Connect to server
		iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);

		if (iResult == SOCKET_ERROR)
		{
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
		}

		freeaddrinfo(result);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Unable to connect to server!\n");
			WSACleanup();
			return false;
		}

		return true;
	};

	// Free the resouces
	void Stop() {
		int iResult = shutdown(ConnectSocket, SD_SEND);

		if (iResult == SOCKET_ERROR)
		{
			printf("shutdown failed: %d\n", WSAGetLastError());
		}

		closesocket(ConnectSocket);
		WSACleanup();
	};

	// Send message to server
	bool Send(char* szMsg)
	{
		
		int iResult = send(ConnectSocket, szMsg, strlen(szMsg), 0);

		if (iResult == SOCKET_ERROR)
		{
			printf("send failed: %d\n", WSAGetLastError());
			Stop();
			return false;
		}

		return true;
	};

	// Receive message from server
	bool Recv()
	{
		char recvbuf[DEFAULT_BUFFER_LENGTH];
		int iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);

		if (iResult &gt; 0)
		{
			char msg[DEFAULT_BUFFER_LENGTH];
			memset(&amp;msg, 0, sizeof(msg));
			strncpy(msg, recvbuf, iResult);

			printf("Received: %s\n", msg);

			return true;
		}


		return false;
	}

private:
	char* szServerName;
	SOCKET ConnectSocket;
};


int main(int argc, CHAR* argv[]) 
{
	std::string msg;

	Client client("127.0.0.1");

	if (!client.Start())
		return 1;

	while(true)
	{
		std::cout &lt;&lt; "Send: ";
		std::getline(std::cin, msg);

		// Close the connection when user enter "close"
		if (msg.compare("close") == 0)
		{
			break;
		}

		client.Send((char*)msg.c_str());
		client.Recv();
	}

	client.Stop();

	getchar();
	return 0;
}</pre><p> 

<li>Enter the following code to main.cpp in the <b>ServerExample</b> project.</li>
 
</p><pre class="crayon-plain-tag">#define WIN32_LEAN_AND_MEAN

#include &lt;WinSock2.h&gt;
#include &lt;WS2tcpip.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;


// link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_PORT "27015"
#define DEFAULT_BUFFER_LENGTH	512

int main() {

	WSADATA wsaData;

	// Initialize Winsock
	int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
	if(iResult != 0)
	{
		printf("WSAStartup failed: %d\n", iResult);
		return 1;
	}

	struct addrinfo	*result = NULL,
					hints;

	ZeroMemory(&amp;hints, sizeof(hints));
	hints.ai_family = AF_INET;		// Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned
	hints.ai_socktype = SOCK_STREAM;	// Requests the socket type to be a stream socket for the TCP protocol
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	// Resolve the local address and port to be used by the server
	iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
	if (iResult != 0)
	{
		printf("getaddrinfo failed: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	SOCKET ListenSocket = INVALID_SOCKET;

	// Create a SOCKET for the server to listen for client connections
	ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);

	if (ListenSocket == INVALID_SOCKET)
	{
		printf("Error at socket(): %d\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	// Setup the TCP listening socket
	iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);

	if (iResult == SOCKET_ERROR)
	{
		printf("bind failed: %d", WSAGetLastError());
		freeaddrinfo(result);
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	freeaddrinfo(result);

	// To listen on a socket
	if ( listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
	{
		printf("listen failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}

	SOCKET ClientSocket;

	ClientSocket = INVALID_SOCKET;

	// Accept a client socket
	ClientSocket = accept(ListenSocket, NULL, NULL);

	if (ClientSocket == INVALID_SOCKET)
	{
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}
	
	char recvbuf[DEFAULT_BUFFER_LENGTH];
	int iSendResult;

	// reveice until the client shutdown the connection
	do {
		iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);
		if (iResult &gt; 0)
		{
			char msg[DEFAULT_BUFFER_LENGTH];
			memset(&amp;msg, 0, sizeof(msg));
			strncpy(msg, recvbuf, iResult);

			printf("Received: %s\n", msg);

			iSendResult = send(ClientSocket, recvbuf, iResult, 0);

			if (iSendResult == SOCKET_ERROR)
			{
				printf("send failed: %d\n", WSAGetLastError());
				closesocket(ClientSocket);
				WSACleanup();
				return 1;
			}

			printf("Bytes sent: %ld\n", iSendResult);
		}
		else if (iResult == 0)
			printf("Connection closed\n");
		else
		{
			printf("recv failed: %d\n", WSAGetLastError());
			closesocket(ClientSocket);
			WSACleanup();
			//return 1;
		}
	} while (iResult &gt; 0);

	// Free the resouces
	closesocket(ListenSocket);
	WSACleanup();

	getchar();
	return 0;
}</pre><p> 

<li>Compile both projects. You may run both client and server application in 2 CMD windows. Enter a message in the client and it will send to the server.</li>
<img class="aligncenter size-full" src="http://www.askyb.com/wp-content/uploads/2013/03/client-and-server.png" alt="Windows Socket Example" />
<li>Download sample source code: <a href="http://www.askyb.com/wp-content/uploads/2013/03/ClientServerExample.zip">ClieServerExample.zip</a></li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/windows-socket/windows-socket-example-tcp-client-and-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL HMAC Hasing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 03:24:23 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2682</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s HMAC hash function. HMAC hashing allow user to hash with a secret key. HMAC can take most of the hash engine in order to hash your data with the secret key. This tutorial will create two C++ example files which will...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s HMAC hash function. HMAC hashing allow user to hash with a secret key. HMAC can take most of the hash engine in order to hash your data with the secret key. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2682"></span>

<p>
<ol>
<li>Here are the openssl HMAC sample source code.<br />
<br />
<strong>Example #1: hmac_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/hmac.h&gt;

int main()
{
    // The key to hash
    char key[] = "012345678";

    // The data that we're going to hash using HMAC
    char data[] = "hello world";
    
    unsigned char* digest;
    
    // Using sha1 hash engine here.
    // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
    digest = HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), NULL, NULL);    

    // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
    // Change the length accordingly with your choosen hash engine
    char mdString[20];
    for(int i = 0; i < 20; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("HMAC digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: hmac_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/hmac.h&gt;

int main() {
    // The secret key for hashing
    const char key[] = "012345678";

    // The data that we're going to hash
    char data[] = "hello world";
    
    // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
    // Change the length accordingly with your choosen hash engine.     
    unsigned char* result;
    unsigned int len = 20;

    result = (unsigned char*)malloc(sizeof(char) * len);

    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);

    // Using sha1 hash engine here.
    // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
    HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
    HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
    HMAC_Final(&ctx, result, &len);
    HMAC_CTX_cleanup(&ctx);

    printf("HMAC digest: ");

    for (int i = 0; i != len; i++)
        printf("%02x", (unsigned int)result[i]);

    printf("\n");

    free(result);

    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> hmac_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
HMAC digest: e19e220122b37b708bfb95aca2577905acabf0c0<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> hmac_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
HMAC digest: e19e220122b37b708bfb95aca2577905acabf0c0</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>OpenSSL RIPEMD Hashing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-ripmd-hashing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-ripmd-hashing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 02:48:07 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2667</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s RIPEMD hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment. Here are the openssl RIPEMD sample source code. Example #1: ripemd_sample1.cpp [crayon-51c23d0998176/] Example #2: ripemd_sample2.cpp [crayon-51c23d0998597/] Let&#8217;s try to compile both sample...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s RIPEMD hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2667"></span>

<p>
<ol>
<li>Here are the openssl RIPEMD sample source code.<br />
<br />
<strong>Example #1: ripemd_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/ripemd.h&gt;

int main()
{
    unsigned char digest[RIPEMD160_DIGEST_LENGTH];
    char string[] = "hello world";
    
    RIPEMD160((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    

    char mdString[RIPEMD160_DIGEST_LENGTH*2+1];

    for(int i = 0; i < RIPEMD160_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("RIPEMD160 digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: ripemd_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/ripemd.h&gt;

int main() {
    unsigned char digest[RIPEMD160_DIGEST_LENGTH];
    const char* string = "hello world";

    RIPEMD160_CTX ctx;
    RIPEMD160_Init(&ctx);
    RIPEMD160_Update(&ctx, string, strlen(string));
    RIPEMD160_Final(digest, &ctx);

    char mdString[RIPEMD160_DIGEST_LENGTH*2+1];
    for (int i = 0; i < RIPEMD160_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("RIPEMD160 digest: %s\n", mdString);

    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> ripemd_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
RIPEMD160 digest: 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> ripemd_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
RIPEMD160 digest: 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-ripmd-hashing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OpenSSL SHA512 Hashing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-sha512-hashing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-sha512-hashing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 02:39:36 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2657</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA512 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment. Here are the openssl SHA512 sample source code. Example #1: sha512_sample1.cpp [crayon-51c23d09991c2/] Example #2: sha512_sample2.cpp [crayon-51c23d09994fd/] Let&#8217;s try to compile both sample...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA512 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2657"></span>

<p>
<ol>
<li>Here are the openssl SHA512 sample source code.<br />
<br />
<strong>Example #1: sha512_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main()
{
    unsigned char digest[SHA512_DIGEST_LENGTH];
    char string[] = "hello world";
    
    SHA512((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    

    char mdString[SHA512_DIGEST_LENGTH*2+1];

    for(int i = 0; i < SHA512_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA512 digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: sha512_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main() {
    unsigned char digest[SHA512_DIGEST_LENGTH];
    const char* string = "hello world"; 

    SHA512_CTX ctx;
    SHA512_Init(&ctx);
    SHA512_Update(&ctx, string, strlen(string));
    SHA512_Final(digest, &ctx);

    char mdString[SHA512_DIGEST_LENGTH*2+1];
    for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA512 digest: %s\n", mdString);


    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha512_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
SHA512 digest: 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha512_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
SHA512 digest: 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-sha512-hashing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL SHA384 Hashing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-sha384-hashing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-sha384-hashing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 02:34:08 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2655</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA384 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment. Here are the openssl SHA384 sample source code. Example #1: sha384_sample1.cpp [crayon-51c23d0999ccf/] Example #2: sha384_sample2.cpp [crayon-51c23d099a0b6/] Let&#8217;s try to compile both sample...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA384 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2655"></span>

<p>
<ol>
<li>Here are the openssl SHA384 sample source code.<br />
<br />
<strong>Example #1: sha384_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main()
{
    unsigned char digest[SHA384_DIGEST_LENGTH];
    char string[] = "hello world";
    
    SHA384((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    

    char mdString[SHA384_DIGEST_LENGTH*2+1];

    for(int i = 0; i < SHA384_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA384 digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: sha384_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main() {
    unsigned char digest[SHA512_DIGEST_LENGTH];
    const char* string = "hello world"; 

    SHA512_CTX ctx;
    SHA384_Init(&ctx);
    SHA384_Update(&ctx, string, strlen(string));
    SHA384_Final(digest, &ctx);

    char mdString[SHA384_DIGEST_LENGTH*2+1];
    for (int i = 0; i < SHA384_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA384 digest: %s\n", mdString);


    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha384_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
SHA384 digest: fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha384_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
SHA384 digest: fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-sha384-hashing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OpenSSL SHA256 Hashing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-sha256-hashing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-sha256-hashing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 02:30:03 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2650</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA256 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment. Here are the openssl SHA256 sample source code. Example #1: sha256_sample1.cpp [crayon-51c23d099ac6e/] Example #2: sha256_sample2.cpp [crayon-51c23d099b055/] Let&#8217;s try to compile both sample...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA256 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2650"></span>

<p>
<ol>
<li>Here are the openssl SHA256 sample source code.<br />
<br />
<strong>Example #1: sha256_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main()
{
    unsigned char digest[SHA256_DIGEST_LENGTH];
    char string[] = "hello world";
    
    SHA256((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    

    char mdString[SHA256_DIGEST_LENGTH*2+1];

    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA256 digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: sha256_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main() {
    unsigned char digest[SHA256_DIGEST_LENGTH];
    const char* string = "hello world"; 

    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, string, strlen(string));
    SHA256_Final(digest, &ctx);

    char mdString[SHA256_DIGEST_LENGTH*2+1];
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA256 digest: %s\n", mdString);


    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha256_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
SHA256 digest: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha256_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
SHA256 digest: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-sha256-hashing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL SHA224 Hashing Example in C++</title>
		<link>http://www.askyb.com/cpp/openssl-sha224-hashing-example-in-cpp/</link>
		<comments>http://www.askyb.com/cpp/openssl-sha224-hashing-example-in-cpp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 02:25:12 +0000</pubDate>
		<dc:creator>askyb</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[hashing]]></category>

		<guid isPermaLink="false">http://www.askyb.com/?p=2646</guid>
		<description><![CDATA[This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA224 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment. Here are the openssl SHA224 sample source code. Example #1: sha224_sample1.cpp [crayon-51c23d099b826/] Example #2: sha224_sample2.cpp [crayon-51c23d099bc0d/] Let&#8217;s try to compile both sample...]]></description>
				<content:encoded><![CDATA[<p>This tutorial will guide you on how to hash a string by using OpenSSL&#8217;s SHA224 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.</p><span id="more-2646"></span>

<p>
<ol>
<li>Here are the openssl SHA224 sample source code.<br />
<br />
<strong>Example #1: sha224_sample1.cpp</strong>
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main()
{
    unsigned char digest[SHA224_DIGEST_LENGTH];
    char string[] = "hello world";
    
    SHA224((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    

    char mdString[SHA224_DIGEST_LENGTH*2+1];

    for(int i = 0; i < SHA224_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA224 digest: %s\n", mdString);

    return 0;
}</pre><p> 

<br />
<strong>Example #2: sha224_sample2.cpp</strong>
 
</p><pre class="crayon-plain-tag">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/sha.h&gt;

int main() {
    unsigned char digest[SHA224_DIGEST_LENGTH];
    const char* string = "hello world"; 

    SHA256_CTX ctx;
    SHA224_Init(&ctx);
    SHA224_Update(&ctx, string, strlen(string));
    SHA224_Final(digest, &ctx);

    char mdString[SHA224_DIGEST_LENGTH*2+1];
    for (int i = 0; i < SHA224_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA224 digest: %s\n", mdString);


    return 0;
}</pre><p> 
</li>
<li>Let&#8217;s try to compile both sample cpp files and you should observe the following output screenshot.

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha224_sample1.cpp <span style="color: #660033;">-o</span> sample1 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample1<br />
SHA224 digest: 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b<br />
~$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> sha224_sample2.cpp <span style="color: #660033;">-o</span> sample2 <span style="color: #660033;">-lcrypto</span><br />
~$ .<span style="color: #000000; font-weight: bold;">/</span>sample2<br />
SHA224 digest: 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b</div></div>

Note: -lcrypto will include the crypto library from openssl
</li>
</ol>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.askyb.com/cpp/openssl-sha224-hashing-example-in-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
