0

我正在尝试在 OS X 上使用 ruby​​Motion 从钥匙串中检索密码

我试过这个:

#   passsword_data_pointer=Pointer.new(:object) #works but empty password
#   password_data_pointer=Pointer.new('^') #makes ruby crash and complain 'Can't find pointer description for type '^'
    password_data=NSMutableData.new #works but empty password

    password_length = Pointer.new('I')
    result=SecKeychainFindGenericPassword (
                                           nil,
                                           "some_service_string".length,
                                           "some_service_string",
                                           "some_username_string".length,
                                           "some_username_string",
                                           password_length,
                                           password_data_pointer,#or password_data.bytes
                                           nil
                                           )

#    password_string=NSMutableData.dataWithBytes(password_data.bytes, length:password_length[0])
    password_string=NSMutableData.dataWithBytes(password_data_pointer, length:password_length[0])

    p password_string

无论我做什么,都无法找回密码。

请帮忙 ; 搜索了很多年,互联网上到处都是 macruby 或 cocoa 或 c 的例子,但没有关于这个主题的 ruby​​motion。

4

1 回答 1

1

我对 SecKeychainFindGenericPassword 不太熟悉,但我知道您需要设置正确的权利才能使用RubyMotion 项目管理指南中讨论的钥匙串。

因此,请确保您的 Rakefile 中有以下行:

app.entitlements['keychain-access-groups'] = [
  app.seed_id + '.' + app.identifier
]

如果您想要更好的钥匙串接口,我使用可以通过 Cocoapods 拉入的SSKeychain可可包装器。

在您的 Gemfile 中:

gem 'cocoapods',        '~> 0.23.0'
gem 'motion-cocoapods', '~> 1.3.6'

同样在 Rakefile 中:

app.pods do
  pod 'SSKeychain', '~> 1.2.0'
end

这是我使用 SSKeychain 存储和检索敏感数据的包装器的简化版本:

class CredentialStore
  SERVICE = 'YOUR_APP_NAME'

  def set_secure_value(value, for_key: key)
    if value
      SSKeychain.setPassword(value, forService: SERVICE, account: key)
    else
      SSKeychain.deletePasswordForService(SERVICE, account: key)
    end
  end

  def secure_value_for_key(key)
    SSKeychain.passwordForService(SERVICE, account: key)
  end
end

如果您还有其他问题,请告诉我。祝你好运!

于 2013-08-15T17:43:10.317 回答