Is there any length limit of string stored in Keychain?

前端 未结 3 736
走了就别回头了
走了就别回头了 2020-12-03 13:31

I want to store some userinfo as a string in Keychain on iOS, so is there any length limit of the string in Keychain?

Thanks.

相关标签:
3条回答
  • 2020-12-03 13:45

    I threw together an iOS app that would do a binary search using this library to interact with the keychain, and determined that the most I could store was an NS(Mutable)String with length 16,777,110. So noticeably less than either the max length of an NSString or the default value of SQLITE_MAX_LENGTH as suggested in other answers.

    EDIT: If you're looking to quickly verify this answer, I'd suggest trying to store a String with length 16,777,110 in the keychain, and then trying one with length 16,777,111. If the first one works and the second one doesn't, this answer is correct.

    0 讨论(0)
  • 2020-12-03 14:04

    It's hard to answer :) It should support the maximum length of NSString

    So the Keychain string can hold a little over 4.2 billion characters same as NSString

    0 讨论(0)
  • 2020-12-03 14:09

    The keychain of iOS is realized as SQLite-database (see here for example). So the max length of a string stored in an SQLite-database could be an upper bound for the max length for a string in the keychain.

    According to this page on sqlite.org the max number of bytes for a string or BLOB is limited by the preprocessor macro SQLITE_MAX_LENGTH, whose default-value is 10^9. So assuming a 16-bit-encoding of the string, the max length of a string would be the half of this value. However, it could be that Apple uses a lower value for SQLITE_MAX_LENGTH.

    0 讨论(0)
提交回复
热议问题