791b971cf5fc5f0da9a41dc5e2a623f6ed9f1804
[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 <stddef.h>
23 #include <dpl/crypto_hash.h>
24 #include <iostream>
25 #include <string>
26
27 void help()
28 {
29     std::cout << "Invalid parameters: crypto_hash [hash_function] [message]" << std::endl;
30     std::cout << "hash_function is one of following: MD2, MD4, MD5, SHA, SHA1, DSS, DSS1, ECDSA, SHA224, SHA256, SHA384, SHA512" << std::endl;
31 }
32
33 int main(int argc, char *argv[])
34 {
35     if (argc != 3)
36     {
37         help();
38         return 0;
39     }
40
41     DPL::Crypto::Hash::Base *crypto;
42     std::string algorithm = argv[1];
43
44     if (algorithm == "MD2")
45         crypto = new DPL::Crypto::Hash::MD2();
46     else if (algorithm == "MD4")
47         crypto = new DPL::Crypto::Hash::MD4();
48     else if (algorithm == "MD5")
49         crypto = new DPL::Crypto::Hash::MD5();
50     else if (algorithm == "SHA")
51         crypto = new DPL::Crypto::Hash::SHA();
52     else if (algorithm == "SHA1")
53         crypto = new DPL::Crypto::Hash::SHA1();
54     else if (algorithm == "DSS")
55         crypto = new DPL::Crypto::Hash::DSS();
56     else if (algorithm == "DSS1")
57         crypto = new DPL::Crypto::Hash::DSS1();
58     else if (algorithm == "ECDSA")
59         crypto = new DPL::Crypto::Hash::ECDSA();
60     else if (algorithm == "SHA224")
61         crypto = new DPL::Crypto::Hash::SHA224();
62     else if (algorithm == "SHA256")
63         crypto = new DPL::Crypto::Hash::SHA256();
64     else if (algorithm == "SHA384")
65         crypto = new DPL::Crypto::Hash::SHA384();
66     else if (algorithm == "SHA512")
67         crypto = new DPL::Crypto::Hash::SHA512();
68     else
69     {
70         help();
71         return 0;
72     }
73
74     crypto->Append(argv[2]);
75     crypto->Finish();
76     
77     std::cout << crypto->ToString() << std::endl;
78
79     delete crypto;
80
81     return 0;
82 }