0fffefac7ec730bbcb2530e672e352a9a4daf24a
[framework/web/wrt-commons.git] / examples / crypto_hash / crypto_hash.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        crypto_hash.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of crypto hash example
21  */
22 #include <dpl/crypto_hash.h>
23 #include <iostream>
24 #include <string>
25
26 void help()
27 {
28     std::cout << "Invalid parameters: crypto_hash [hash_function] [message]" << std::endl;
29     std::cout << "hash_function is one of following: MD2, MD4, MD5, SHA, SHA1, DSS, DSS1, ECDSA, SHA224, SHA256, SHA384, SHA512" << std::endl;
30 }
31
32 int main(int argc, char *argv[])
33 {
34     if (argc != 3)
35     {
36         help();
37         return 0;
38     }
39
40     DPL::Crypto::Hash::Base *crypto;
41     std::string algorithm = argv[1];
42
43     if (algorithm == "MD2")
44         crypto = new DPL::Crypto::Hash::MD2();
45     else if (algorithm == "MD4")
46         crypto = new DPL::Crypto::Hash::MD4();
47     else if (algorithm == "MD5")
48         crypto = new DPL::Crypto::Hash::MD5();
49     else if (algorithm == "SHA")
50         crypto = new DPL::Crypto::Hash::SHA();
51     else if (algorithm == "SHA1")
52         crypto = new DPL::Crypto::Hash::SHA1();
53     else if (algorithm == "DSS")
54         crypto = new DPL::Crypto::Hash::DSS();
55     else if (algorithm == "DSS1")
56         crypto = new DPL::Crypto::Hash::DSS1();
57     else if (algorithm == "ECDSA")
58         crypto = new DPL::Crypto::Hash::ECDSA();
59     else if (algorithm == "SHA224")
60         crypto = new DPL::Crypto::Hash::SHA224();
61     else if (algorithm == "SHA256")
62         crypto = new DPL::Crypto::Hash::SHA256();
63     else if (algorithm == "SHA384")
64         crypto = new DPL::Crypto::Hash::SHA384();
65     else if (algorithm == "SHA512")
66         crypto = new DPL::Crypto::Hash::SHA512();
67     else
68     {
69         help();
70         return 0;
71     }
72
73     crypto->Append(argv[2]);
74     crypto->Finish();
75     
76     std::cout << crypto->ToString() << std::endl;
77
78     delete crypto;
79
80     return 0;
81 }