5

I did generate a signature using the below code but, I want a signature in IEEE P1336 (length 80) format

           guard let signData = SecKeyCreateSignature(
               key,
               SecKeyAlgorithm.ecdsaSignatureMessageX962SHA256,
               signatureString.data(using: .utf8)! as CFData, &error) else {
               let e = error!.takeRetainedValue() as Error

               print("Signing Error \(  e.localizedDescription)")
               return nil
           }
           let signedData = signData as Data
           let signedString = signedData.base64EncodedString(options: [])

1 Answers1

0

I am not an expert in iOS. But I fixed this issue for a signature generated by iOS. You can create this format by your own. e.g. for SHA-256 hashed signature. JavaScript sample

function parseASN1ToIEEEP1363(signature) {
    const buffer = new ArrayBuffer(signature.length);
    const int8View = new Int8Array(buffer);
    for (let i = 0, strLen = signature.length; i < strLen; i++) {
      int8View[i] = signature.charCodeAt(i);
    }
    //Currently these bytes getting for SHA256. for other hashings need to make it dynamic
    const r = new Int8Array(buffer.slice(4, 36));
    const s = new Int8Array(buffer.slice(39));
    return appendBuffer(r, s);
}

function appendBuffer(buffer1, buffer2) {
  var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
  tmp.set(new Uint8Array(buffer1), 0);
  tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
  return tmp;
};
Sajid Hussain
  • 400
  • 3
  • 10