Skip to content
Browse files

library and python ctypes wrapper around key generation

  • Loading branch information...
1 parent cd7d634 commit ecf45319a74078029223d1bd1d83f26f60991876 @toffaletti committed May 9, 2012
Showing with 128 additions and 0 deletions.
  1. +3 −0 CMakeLists.txt
  2. +90 −0 keygen.cc
  3. +35 −0 test_libkeygen.py
View
3 CMakeLists.txt
@@ -15,3 +15,6 @@ target_link_libraries(credit-client ten boost_program_options)
add_executable(credit-server credit-server.cc)
target_link_libraries(credit-server ten boost_program_options)
+
+add_library(keygen SHARED keygen.cc)
+target_link_libraries(keygen ten)
View
90 keygen.cc
@@ -0,0 +1,90 @@
+#include "keygen.hh"
+
+extern "C" {
+ extern int key_generate(
+ const char *secret,
+ uint64_t org_id,
+ uint16_t app_id,
+ uint32_t credits,
+ time_t expire_date,
+ uint8_t flags,
+ char key[40]
+ );
+
+ extern int key_verify(
+ const char *secret,
+ const char key[40],
+ uint64_t *org_id,
+ uint16_t *app_id,
+ uint32_t *credits,
+ time_t *expire_date,
+ uint8_t *flags
+ );
+}
+
+extern "C" {
+ int key_generate(
+ const char *secret,
+ uint64_t org_id,
+ uint16_t app_id,
+ uint32_t credits,
+ time_t expire_time,
+ uint8_t flags,
+ char key[40]
+ )
+ {
+ key_engine eng(secret);
+ memset(key, 0, sizeof(key));
+ try {
+ expire_date_t expires = no_expire;
+ if (expire_time) {
+ struct tm t = {};
+ gmtime_r(&expire_time, &t);
+ expires.year = t.tm_year;
+ expires.month = t.tm_mon;
+ expires.day = t.tm_mday;
+ }
+ std::string k = eng.generate(org_id, app_id, credits, expires, flags);
+ memcpy(key, k.data(), k.size());
+ return 0;
+ } catch (...) {
+ return -1;
+ }
+ return -2;
+ }
+
+ int key_verify(
+ const char *secret,
+ const char key[40],
+ uint64_t *org_id,
+ uint16_t *app_id,
+ uint32_t *credits,
+ time_t *expire_date,
+ uint8_t *flags
+ )
+ {
+ apikey k;
+ key_engine eng(secret);
+ bool valid = eng.verify(key, k);
+ if (valid) {
+ if (org_id)
+ *org_id = k.data.org_id;
+ if (app_id)
+ *app_id = k.data.app_id;
+ if (credits)
+ *credits = k.data.credits;
+ if (expire_date) {
+ struct tm t = {};
+ t.tm_year = k.data.expires.year;
+ t.tm_mon = k.data.expires.month;
+ t.tm_mday = k.data.expires.day;
+ *expire_date = mktime(&t);
+ }
+ if (flags)
+ *flags = k.data.flags;
+ return 1;
+ }
+ return 0;
+ }
+
+}
View
35 test_libkeygen.py
@@ -0,0 +1,35 @@
+from ctypes import *
+from datetime import date
+import time
+
+lib = cdll.LoadLibrary("./libkeygen.so")
+
+def key_generate(secret, org_id, app_id, credits=0, expire_date=None, flags=0):
+ key = create_string_buffer(40)
+ expire_time = 0L
+ if expire_date:
+ expire_time = long(time.mktime(expire_date.timetuple()))
+ status = lib.key_generate(secret, org_id, app_id, credits, expire_time, flags,
+ key)
+ if status != 0:
+ raise Exception("bad")
+ return key.raw
+
+def key_verify(secret, key):
+ org_id = c_ulonglong()
+ app_id = c_ushort()
+ credits = c_ulong()
+ expire_time = c_ulonglong()
+ flags = c_ubyte()
+ status = lib.key_verify(secret, key, byref(org_id), byref(app_id),
+ byref(credits), byref(expire_time), byref(flags))
+ if status:
+ return (org_id.value, app_id.value, credits.value,
+ date.fromtimestamp(expire_time.value),
+ flags.value)
+ return None
+
+if __name__ == '__main__':
+ key = key_generate("weee", 1, 1, 500, date(1980, 7, 12), 3)
+ print key
+ print key_verify("weee", key)

0 comments on commit ecf4531

Please sign in to comment.
Something went wrong with that request. Please try again.