Is there any length limit of string stored in Keychain?
Asked Answered
S

3

38

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.

Stereochrome answered 21/11, 2012 at 8:5 Comment(0)
B
66

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.

Boneblack answered 11/6, 2014 at 14:32 Comment(2)
And what about performance ? How long does it take to store / get a long String from the Keychain ?Excess
Btw, 16777110 bytes ~= 16 MBMaiocco
A
17

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

Acetanilide answered 21/11, 2012 at 8:14 Comment(2)
That's a pretty big keychainPhylactery
According to Mark's answer, testing this experimentally yields a much lower limit.Cassareep
B
6

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.

Bigotry answered 10/8, 2013 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.