Extract crypto algorithm field and add utility functions

Password field of a user in /etc/shadow contains 3 parts:
[Crypt algorithm, Salt, encrypted password]

Example: A value of "1" in crypt algorithm maps to MD5

Need to use the same crypt algorithm that is already used
before when the new password is to be updated.

Change-Id: Ib7d8e0ad6f3bcce30f5c2be89b4e033230c07bf4
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/user.hpp b/user.hpp
index 25c7e62..aa40820 100644
--- a/user.hpp
+++ b/user.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
-#include <string>
+#include <cstring>
+#include <experimental/filesystem>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
 #include <xyz/openbmc_project/User/Password/server.hpp>
@@ -9,6 +10,9 @@
 namespace user
 {
 
+using CryptAlgo = std::string;
+
+namespace fs = std::experimental::filesystem;
 namespace Base = sdbusplus::xyz::openbmc_project::User::server;
 using Interface = sdbusplus::server::object::object<Base::Password>;
 
@@ -35,7 +39,8 @@
         User(sdbusplus::bus::bus& bus, const char* path)
             : Interface(bus, path),
               bus(bus),
-              path(path)
+              path(path),
+              user(fs::path(path).filename())
         {
             // Do nothing
         }
@@ -56,6 +61,34 @@
 
         /** @brief object path */
         const std::string& path;
+
+        /** @brief User id extracted from object path */
+        const std::string user;
+
+        /** @brief Extracts crypto number from the shadow entry for user
+         *
+         *  @param[in] spPwdp - sp_pwdp of struct spwd
+         */
+        static CryptAlgo getCryptField(char* spPwdp);
+
+        /** @brief Generates one-way hash based on salt and password
+         *
+         *  @param[in] password - clear text password
+         *  @param[in] salt     - Combination of crypto method and salt
+         *                        Eg: $1$HELLO$, where in 1 is crypto method
+         *                        and HELLO is salt
+         */
+        static std::string generateHash(const std::string& password,
+                                        const std::string& salt);
+
+        /** @brief returns salt string with $ delimiter.
+         *         Eg: If crypt is 1 and salt is HELLO, returns $1$HELLO$
+         *
+         *  @param[in] crypt - Crypt number in string
+         *  @param[in] salt  - salt
+         */
+        static std::string getSaltString(const std::string& crypt,
+                                         const std::string& salt);
 };
 
 } // namespace user