我需要以下方面的一些指导。请指导我:
客观的: 存储和访问给定用户的文件加密密钥。每个文件都有一个单独的加密密钥。因此,想要加密 10 个文件的用户将在密钥环上存储 10 个不同的密钥。
问题:我面临两个问题:1. 存储和访问密钥时获取垃圾数据 2. 回调函数未被调用
执行:
#include <stdio.h>
#include <gnome-keyring-1/gnome-keyring.h>
#include <gnome-keyring-1/gnome-keyring-memory.h>
GnomeKeyringPasswordSchema my_schema = {
GNOME_KEYRING_ITEM_GENERIC_SECRET,
{
{ "user", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ "file", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ NULL, 0 }
}
};
void
stored_password (GnomeKeyringResult res, gpointer user_data)
{
printf( "DEBUG: StoredPass - BFR\n" );
/* user_data will be the same as was passed to gnome_keyring_store_password() */
if (res == GNOME_KEYRING_RESULT_OK)
printf ("password saved successfully!\n");
else
printf ("couldn't save password: %s", gnome_keyring_result_to_message (res));
printf( "DEBUG: StoredPass - AFR\n" );
}
void
save_my_password()
{
GnomeKeyringResult res;
gpointer gp = NULL, gp2 = NULL;
printf( "DEBUG: SaveMyPass - BFR\n" );
gp = gnome_keyring_store_password ( &my_schema, /* The password type */
GNOME_KEYRING_DEFAULT, /* Where to save it */
"My file encryption password", /* Password description, displayed to user */
"test1000", /* The password itself */
stored_password, /* A function called when complete */
gp2, NULL, /* User data for callback, and destroy notify */
/* Attributes */
"user", "piyush",
"file", "abc.txt",
NULL); /* Always end with NULL */
printf( "DEBUG: SaveMyPass - AFR\n" );
g_assert( gp != NULL );
printf( "%p %s\n", gp, (char *)gp );
}
/* A callback called when the operation completes */
void
found_password (GnomeKeyringResult res, const gchar* password, gpointer user_data)
{
/* user_data will be the same as was passed to gnome_keyring_find_password() */
printf( "DEBUG: FoundPass - BFR\n" );
if (res == GNOME_KEYRING_RESULT_OK)
printf ("password found was: %s\n", password);
else
printf ("couldn't find password: %s", gnome_keyring_result_to_message (res));
/* Once this function returns |password| will be freed */
printf( "DEBUG: FoundPass - AFR\n" );
}
void
find_my_password()
{
printf( "DEBUG: FindMyPass - BFR\n" );
gpointer gp = gnome_keyring_find_password ( &my_schema, /* The password type */
found_password, /* A function called when complete */
NULL, NULL, /* User data for callback, and destroy notify */
/* Attributes */
"user", "piyush",
"file", "abc.txt",
NULL); /* Always end with NULL */
printf( "DEBUG: FindMyPass - AFR\n" );
g_assert( gp != NULL );
printf( "%p %s\n", gp, (char *)gp );
}
int main() {
save_my_password();
find_my_password();
}
编译:gcc gnomekey.c -o gnomekey -lgnome-keyringpkg-config --cflags --libs gnome-keyring-1
输出:./gnomekey
DEBUG: SaveMyPass - BFR
DEBUG: SaveMyPass - AFR
0x8676800 <junk-data>
DEBUG: FindMyPass - BFR
DEBUG: FindMyPass - AFR
0x8676828 <junk-data>
答案1
看起来您正在使用 gnome-keyring 的异步 API。这些 API 依赖于glib
正在运行的事件循环来接收来自密钥环守护程序的回复。
您的应用程序似乎没有运行事件循环,并且在对您的密钥环守护程序请求的响应得到答复之前退出,这可以解释您所看到的问题。
有两种方法可以解决此问题:
- 修改您的应用程序来运行主循环,以便它可以接收响应。
- 使用 API 的同步版本(即
gnome_keyring_store_password_sync
和gnome_keyring_find_password_sync
),因此库调用块来等待响应。
如果您正在编写图形应用程序,那么您可能已经在执行 (1),因此您使用的 API 是正确的。这可能只是修改测试程序以运行主循环的情况。
如果您正在编写命令行脚本,那么阻塞行为可能不是一个大问题,因此使用同步 API 将使您的生活变得轻松很多。