iOS钥匙串不会添加带有硬编码字符串的查询

时间:2018-08-02 08:42:21

标签: ios keychain

我正在按照Apple文档的说明,向位于此处的钥匙串添加密码-> https://developer.apple.com/documentation/security/keychain_services/keychain_items/adding_a_password_to_the_keychain

当我运行以下代码时,它可以按预期运行,并且状态返回为0。

        let credentials = Credentials(username: "testUserName", password: "testPassword")
        let server = "www.example.com"
        let account = credentials.username
        let password = credentials.password.data(using: String.Encoding.utf8)!
        let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
                                    kSecAttrAccount as String: account,
                                    kSecAttrServer as String: server,
                                    kSecValueData as String: password]

        let status = SecItemAdd(query as CFDictionary, nil)
        print(status)

当我用硬编码字符串修改代码时,它的状态为-50失败。

        let credentials = Credentials(username: "testUserName", password: "testPassword")
        let server = "www.example.com"
        //let account = credentials.username
        //let password = credentials.password.data(using: String.Encoding.utf8)!
        let account = "testUserName"
        let password = "testPassword"
        let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
                                    kSecAttrAccount as String: account,
                                    kSecAttrServer as String: server,
                                    kSecValueData as String: password]

        let status = SecItemAdd(query as CFDictionary, nil)
        print("Keychain Save Status: \(status)")

有人可以向我解释吗?我还尝试使用let account = "testUsername".utf8将字符串显式设置为utf8格式。对我来说,如果该值是有效的字符串,这将失败。

还有没有人链接到状态码说明?我找到了说明,但没有给出相关的数字代码https://developer.apple.com/documentation/security/1542001-security_framework_result_codes

2 个答案:

答案 0 :(得分:0)

是的,我也知道我的回答。根据我自己的经验,我在Swift和Objective-C. Happy and Lazy Coding中使用下面的钥匙串包装器! :)

迅速-锁匠-https://github.com/matthewpalmer/Locksmith

Objective-C-SSKeychain-https://github.com/samsoffes/sskeychain

  //Generate Device UUID
            func CreateApplicationDeviceUUID() -> String{

                let DeviceUUID = NSUUID().uuidString
                print("DeviceUUD==\(DeviceUUID)")
                return DeviceUUID
            }



        //Retrive Device Unique UUID
                let keyChainID = Locksmith.loadDataForUserAccount(userAccount: Bundle.main.object(forInfoDictionaryKey:"CFBundleName") as! String)

                let retriveuuid = keyChainID?[RDGlobalFunction.deviceAppUUID] //RDGlobalFunction.deviceAppUUID is a Key of KeyChain Value Storage

                if(retriveuuid == nil){

                    let uuid = CreateApplicationDeviceUUID()

                    do{
                        try Locksmith.saveData(data: [RDGlobalFunction.deviceAppUUID : uuid], forUserAccount: Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String) //Locksmith - SSkeyChain Thirdparty KeyChain Wrapper
                    }catch{
                        //Catch Error 
                    }

                }

其他参考链接:

  1. https://www.raywenderlich.com/179924/secure-ios-user-data-keychain-biometrics-face-id-touch-id

  2. https://medium.com/ios-os-x-development/securing-user-data-with-keychain-for-ios-e720e0f9a8e2

  3. https://code.tutsplus.com/tutorials/securing-ios-data-at-rest--cms-28528

答案 1 :(得分:0)

我发现如果我改变

library(shiny)
library(ggplot2)
library(imager)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("plot images"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         fileInput("image",
                     "Select your image:", placeholder = "No file selected")
      ),

      # Show a plot of the generated distribution
      mainPanel(
        plotOutput("photo")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$photo <- renderPlot({
    # Ensure the values for 'image' are available
    # If not, the operation is stopped by raising a "silent" exception
    req(input$image)
    # Get edges of image with imager::cannyEdges
    img <- cannyEdges(input$image)
    # img is now a logical array with 4 dimensions but we only want 2 - discard two of the dimensions
    img <- img[, , 1, 1]
    # Convert the matrix into a list of coordinates
    coords <- which(img, arr.ind = T)
    # Turn the coordinates into a dataframe
    df <- data.frame(x = coords[, 1], y = coords[, 2])
    # Plot the coordinates
    ggplot(df, aes(x, -y)) +
      geom_point()
      })
}

# Run the application 
shinyApp(ui = ui, server = server)

let password = "testPassword"

然后它将按预期工作。看来kSecValueData参数必须是utf8编码的字符串。