diff --git a/Atom-Linux/Makefile b/Atom-Linux/Makefile index 353c27d98..b5bd7ba41 100644 --- a/Atom-Linux/Makefile +++ b/Atom-Linux/Makefile @@ -17,6 +17,7 @@ CXXFLAGS := -Werror \ -I/usr/include/pango-1.0 \ -I/usr/include/gio-unix-2.0/ \ -I/usr/include/glib-2.0 \ + -I/usr/include/openssl \ -I/usr/lib/glib-2.0/include \ -I../cef \ -O2 \ @@ -52,6 +53,8 @@ LIBS := -lX11 \ -lgthread-2.0 \ -lrt \ -lglib-2.0 \ + -lssl \ + -lcrypto \ -lcef \ -lcef_dll_wrapper diff --git a/Atom-Linux/native_handler.cpp b/Atom-Linux/native_handler.cpp index db1f4c55c..34007f926 100644 --- a/Atom-Linux/native_handler.cpp +++ b/Atom-Linux/native_handler.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #define BUFFER_SIZE 8192 @@ -55,7 +56,7 @@ NativeHandler::NativeHandler() : "absolute", "list", "isFile", "isDirectory", "remove", "asyncList", "open", "openDialog", "quit", "writeToPasteboard", "readFromPasteboard", "showDevTools", "newWindow", "saveDialog", "exit", "watchPath", - "unwatchPath", "makeDirectory", "move", "moveToTrash" }; + "unwatchPath", "makeDirectory", "move", "moveToTrash", "md5ForPath" }; int arrayLength = sizeof(functionNames) / sizeof(const char *); for (int i = 0; i < arrayLength; i++) { const char *functionName = functionNames[i]; @@ -437,6 +438,45 @@ void NativeHandler::UnwatchPath(const CefString& name, inotify_rm_watch(notifyFd, descriptor); } +void NativeHandler::Digest(const CefString& name, CefRefPtr object, + const CefV8ValueList& arguments, CefRefPtr& retval, + CefString& exception) { + string path = arguments[0]->GetStringValue().ToString(); + + int fd = open(path.c_str(), O_RDONLY); + if (fd < 0) + return; + + const EVP_MD *md; + OpenSSL_add_all_digests(); + md = EVP_get_digestbyname("md5"); + if (!md) + return; + + EVP_MD_CTX context; + EVP_MD_CTX_init(&context); + EVP_DigestInit_ex(&context, md, NULL); + + char buffer[8192]; + int r; + while ((r = read(fd, buffer, sizeof buffer)) > 0) + EVP_DigestUpdate(&context, buffer, r); + close(fd); + + unsigned char value[EVP_MAX_MD_SIZE]; + unsigned int length; + EVP_DigestFinal_ex(&context, value, &length); + EVP_MD_CTX_cleanup(&context); + + stringstream md5; + char hex[3]; + for (uint i = 0; i < length; i++) { + sprintf(hex, "%02x", value[i]); + md5 << hex; + } + retval = CefV8Value::CreateString(md5.str()); +} + bool NativeHandler::Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) { @@ -478,6 +518,8 @@ bool NativeHandler::Execute(const CefString& name, CefRefPtr object, WatchPath(name, object, arguments, retval, exception); else if (name == "unwatchPath") UnwatchPath(name, object, arguments, retval, exception); + else if (name == "md5ForPath") + Digest(name, object, arguments, retval, exception); else cout << "Unhandled -> " + name.ToString() << " : " << arguments[0]->GetStringValue().ToString() << endl; diff --git a/Atom-Linux/native_handler.h b/Atom-Linux/native_handler.h index 0685991bf..1073297a1 100644 --- a/Atom-Linux/native_handler.h +++ b/Atom-Linux/native_handler.h @@ -117,6 +117,10 @@ private: void UnwatchPath(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); + + void Digest(const CefString& name, CefRefPtr object, + const CefV8ValueList& arguments, CefRefPtr& retval, + CefString& exception); }; #endif