The CBOR library for Delphi is returning a different answer than Nodejs

141 Views Asked by At

We are decoding a QR code and receive an unexpected (incorrect) answer from the Delphi library. the Nodejs is the expected response.

Delphi result at map position 192 text converted to Ascii 101,110,86,84,108,118,79,82,70,53,102,111,82,68,103,99,57,70,89,120,84,67,53,77,54,48,118,55,102,110,88,83,113,116, 116,75,99,52,117,78,84,55,53,101,51,118,115,82,95,104,85,48,75,65,101,119,116,95,107,50,108,74,102,45,84,81,122,65,122,121,109,105,50,101,107,90,85,67,49,118,116,80,108,70,87,103,77

Nodejs result and the expected answer at map position 192 122, 117, 83, 150, 243, 145, 23, 151, 232, 68, 56, 28, 244, 86, 49, 76, 46, 76, 235, 75, 251, 126, 117, 210, 170, 219, 74, 115, 139, 141, 79, 190, 94,222, 251, 17, 254, 21, 52, 40, 7, 176, 183, 249, 54, 148, 151, 254, 77, 12, 192, 207, 41, 162, 217,233, 25, 80, 45, 111, 180, 249, 69, 90, 3

Decoding this QR string to a map need the result at 192 3ECQDIYBQEIRQ5MBDBXBQ7FIAGBRCFQUDBYIEA2Q2C5A4FSECFA4PKHNWFC5NRLIMIMHF JIYOQMJMGFWDJRDW3F2DC7BRRAYYBMEC6TVKOLPHEIXS7UEIOA46RLDCTBOJTVUX636 OXJKVW2KOOFY2T56L3PPWEP6CU2CQB5QW74TNFEX7ZGQZQGPFGRNT2IZKAWW7NHZIVN AGGGCQMMQIAKYELWQDEP3OFVEUZQ6YX5WINVT6YRF5P6BBJ7NUYGSWS5V435T5T53CI D7LARO2AMR7NYWUSTGD3C7WZBWWP3CEXV7YEFH5WTA2K2LWXTPWPWPXMJAP4MHLAQYN QMIEGE6UIMIUWCXPLMQKANEAAKRQ2QYMQMGYST23ECQDIQACYMHYFIYNZMDW6QAAARI 2BF6MUQEYTGQQJUPRSJZIF7O6NYGCM3RG465H2UOKF6OEQX6BGS55OD23CFVZBNFT6O 25L2TUF2PVJ3CXAEF63FRRFAVDCRBUYVUW24RRJA2MI5WZOIYVCBBSBABLARO2AMR7N YWUSTGD3C7WZBWWP3CEXV7YEFH5WTA2K2LWXTPWPWPXMJAP4

can someone help on this?

Here is Delphi Code:


s:= '3ECQDIYBQEIRQ5MBDBXBQ7FIAGBRCFQUDBYIEA2Q2C5A4FSECFA4PKHNWFC5NRLIMIMHF';
 s:= s+'JIYOQMJMGFWDJRDW3F2DC7BRRAYYBMEC6TVKOLPHEIXS7UEIOA46RLDCTBOJTVUX636';
 s:= s+'OXJKVW2KOOFY2T56L3PPWEP6CU2CQB5QW74TNFEX7ZGQZQGPFGRNT2IZKAWW7NHZIVN';
 s:= s+'AGGGCQMMQIAKYELWQDEP3OFVEUZQ6YX5WINVT6YRF5P6BBJ7NUYGSWS5V435T5T53CI';
 s:= s+'D7LARO2AMR7NYWUSTGD3C7WZBWWP3CEXV7YEFH5WTA2K2LWXTPWPWPXMJAP4MHLAQYN';
 s:= s+'QMIEGE6UIMIUWCXPLMQKANEAAKRQ2QYMQMGYST23ECQDIQACYMHYFIYNZMDW6QAAARI';
 s:= s+'2BF6MUQEYTGQQJUPRSJZIF7O6NYGCM3RG465H2UOKF6OEQX6BGS55OD23CFVZBNFT6O';
 s:= s+'25L2TUF2PVJ3CXAEF63FRRFAVDCRBUYVUW24RRJA2MI5WZOIYVCBBSBABLARO2AMR7N';
 s:= s+'YWUSTGD3C7WZBWWP3CEXV7YEFH5WTA2K2LWXTPWPWPXMJAP4';

//Decode Base32
 bytes := TBase32.Rfc4648.Decode( s );

//remove the first three bytes and store in the resulting arry cborBytes2 := copy(bytes,3);

 //get Cbor Map
 items := TCborDecoding.DecodeData( @cborBytes2[0],Length(cborBytes2) );
 memLog.Lines.Add('Found type ' + intToStr(Integer(items.CBorType)));

 case items.cborType of
   majMap:         MemLog.Lines.Add('Map: Length: ' + IntToStr( (items as TCborMap).Count) + ': ' + items.ToString);
   majUnsignedInt: MemLog.Lines.Add('UINt: ' + items.ToString );
   majNegInt:      MemLog.Lines.Add('negINt: ' + items.ToString );
   majByteStr:     rawData := items.ToString;
   majUTFEncStr:   MemLog.Lines.Add('UTF8: ' + items.ToString);
   majArray:       MemLog.Lines.Add('ARR: Length: ' + IntToStr( (items as TCborArr).Count ) + ': ' + items.ToString);
   majTag:         MemLog.Lines.Add('Tag - not implemented');
   majFloat:       MemLog.Lines.Add('Float: ' + items.ToString );
 end;

 itemsMap := ((items as TCborMap).ValueByName['124']) as TCborMap;
 bytesAsStr := (itemsMap.ValueByName['114'] as TCborMap).ValueByName['192'].ToString;

 memo1.Lines.Add(bytesAsStr);

Here is the node.js code

base32EncodedCredential = '3ECQDIYACEMHIGDODB6KQAMDCELBIGDQQIBVAGEOQRICNHQR5O2ULU3JFTZVHGAYOKSRQ5AYSYMLMGTBB36NUGF6DDCBRQCYIF5LYJB7VTXLGIZHZ6FP5B7X556XIOMDYWEO6PIGYWPRJEKPB2QJNWBW637IEAWAP544RL7Q6ZSNE5WTP4LQ523UFZBFGNH5BASK6JXGBQMMFAYZAQAVQIXNAFMX2WWF3ZONWCHPZQUYKDPQ2P6JGUMQXBXKXPVV5MDIQTNWUOXOYWBC5UAVS7K2YXPFZWYI57GCTBIN6DJ7ZE2RSC4G5K56WXVQNCCNW2R253AYOWBBQ3AYQIMJ5IQYRJMFG6WZAUA2IAIVBECAOST23ECQDIQBCYCRQQIILA5XUAAAVPLEEDLCRRJSC5XPBXLSBX3UQJELQCGUXFBFZD2FVNCLKAU75LFCPDKPZUWURTPWC762YD4ZNALVP7EN45FP3JJOEQMJIFIYUINGBVHE64MKIGTALONPOGFIQIMQIAKYELWQCWL5LLC54XG3BDX4YKMFBXYNH7ETKGILQ3VLX226WBUIJW3KHLXM';

const cbor = require('cbor'); const base32Decode = require('base32-decode')

const cborArrayBuffer = base32Decode(base32EncodedCredential, 'RFC4648');

const cborBytes = new Uint8Array(cborArrayBuffer.slice(3));

console.log(cborBytes);

const cborMap = cbor.decode(cborBytes,{useMaps:true});

const vcMap = cborMap.get(124);

console.log(vcMap.get(114).get(192));

const proofValue = convertToMultibase(vcMap.get(114).get(192));

1

There are 1 best solutions below

0
On

the creator of the library Mike answered with the following

As far as I can see is the issue some automatic string convertion. Try to replace the items by:

// instead of using a widestring use the bytes as intended by the TCborByteString... bt := (((itemsMap.ValueByName['114'] as TCborMap).ValueByName['192']) as TCborByteString).ToBytes;

for i := 0 to Length(bt) - 1 do Writeln(bt[i]);

We found that this worked