Post

Replies

Boosts

Views

Activity

Reply to Getting SecKey from a pem file
Oops the first line was missing... func extractKeyFromASN1(_ keyBytes: Data) -> Data? { guard keyBytes.count > 0 else { return nil } var index = 22 // ASN.1 struct must have 0x04 at byte 22 guard keyBytes[index] == 0x04 else { return nil } index += 1 var keyLength = Int(keyBytes[index]) index += 1 let isLengthCodedInMultiBytes = keyLength & 0x80 // higth length bit if isLengthCodedInMultiBytes == 0 { keyLength = keyLength & 0x7f // length is coded in 7 low bits } else { var byteCount = Int(keyLength & 0x7f) // otherwise, number of bytes is coded in 7 low bits guard byteCount + index <= keyBytes.count else { return nil } keyLength = 0 while (byteCount > 0) { keyLength = (keyLength * 256) + Int(keyBytes[index]) index += 1 byteCount -= 1 } } return keyBytes[index..<index+keyLength] }
Jan ’25
Reply to Getting SecKey from a pem file
Hello, function to extract the key data guard keyBytes.count > 0 else { return nil } var index = 22 // ASN.1 struct must have 0x04 at byte 22 guard keyBytes[index] == 0x04 else { return nil } index += 1 var keyLength = Int(keyBytes[index]) index += 1 let isLengthCodedInMultiBytes = keyLength & 0x80 // higth length bit if isLengthCodedInMultiBytes == 0 { keyLength = keyLength & 0x7f // length is coded in 7 low bits } else { var byteCount = Int(keyLength & 0x7f) // otherwise, number of bytes is coded in 7 low bits guard byteCount + index <= keyBytes.count else { return nil } keyLength = 0 while (byteCount > 0) { keyLength = (keyLength * 256) + Int(keyBytes[index]) index += 1 byteCount -= 1 } } return keyBytes[index..<index+keyLength] }
Jan ’25