Skip to content

Commit

Permalink
Validation: Calculate the checksum and compare it to the one given in…
Browse files Browse the repository at this point in the history
… the FIX message

Also put correct checksums in the examples
  • Loading branch information
whatthefrog committed Mar 15, 2015
1 parent 9ccf78f commit d079784
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
41 changes: 41 additions & 0 deletions scripts/app/FixParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,47 @@ define(
}
);

// Checksum(10) verification
_.each(
messages,
function(message)
{
var field_checksum = undefined;
sum = 0;

_.each(
message.fields,
function(field)
{
if ( field.fieldId == FIELD_CHECKSUM ) {
field_checksum = field;
return;
}

for ( var i = 0 ; i < field.raw.length ; i++ ) {
sum += field.raw.charCodeAt(i);
}
}
);

// Modulo 256 + pad up to 3 characters with zero
sum = ("00" + (sum % 256)).slice(-3);

if ( ! field_checksum ) {
return;
}

if ( field_checksum.value == sum ) {
field_checksum.classes += ' valid';
field_checksum.decodedValue = 'Valid';
}
else {
field_checksum.classes += ' invalid';
field_checksum.decodedValue = '/!\\ Invalid (expected ' + sum + ')';
}
}
);

return messages;
};

Expand Down
12 changes: 6 additions & 6 deletions scripts/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ define(
$('#clear').click(function() { input.val(''); return false; });

var sampleMessages = [
'outgoing: 8=FIX.4.2|9=178|35=AE|56=LSEHub|49=BROKERX|128=LSETR|34=2175|52=20120126-15:15:54|918=GBP|31=89.0000000|64=20120126|828=1|60=20120126-13:32:49|32=6|22=4|571=124|43=N|570=N|150=0|48=GB0007188757|10=017|\n' +
'incoming: 8=FIX.4.2|9=112|35=AR|49=LSEHub|56=BROKERX|115=LSETR|34=2006|52=20120126-15:15:54|370=20120126-15:15:54.822|571=124|150=0|939=0|10=126|',
'outgoing: 8=FIX.4.2|9=130|35=AE|49=LSEHub|56=LSETR|115=BROKERX|34=2287|43=N|52=20120330-12:14:09|370=20120330-12:14:09.816|571=00008661533TRLO1-1-1-0|150=H|10=113|\n' +
'incoming: 8=FIX.4.2|9=109|35=AR|34=2486|49=LSETR|52=20120330-12:14:10.379|56=LSEHub|128=BROKERX|150=H|571=00008661533TRLO1-1-1-0|939=0|10=103|',
'outgoing: 8=FIX.4.2|9=175|35=AE|49=BROKERX|56=LSEHub|34=17|52=20120202-16:04:44|128=LSETR|918=EUR|31=89.0000000|64=20120202|828=1007|60=20120202-16:04:44|32=6|22=4|571=1698|570=N|150=0|48=AT0000785555|10=087|\n' +
'incoming: 8=FIX.4.2|9=111|35=AR|49=LSEHub|56=BROKERX|115=LSETR|34=16|52=20120202-16:04:44|370=20120202-16:04:45.257|571=1698|150=0|939=0|10=026|'
'outgoing: 8=FIX.4.2|9=178|35=AE|56=LSEHub|49=BROKERX|128=LSETR|34=2175|52=20120126-15:15:54|918=GBP|31=89.0000000|64=20120126|828=1|60=20120126-13:32:49|32=6|22=4|571=124|43=N|570=N|150=0|48=GB0007188757|10=206|\n' +
'incoming: 8=FIX.4.2|9=112|35=AR|49=LSEHub|56=BROKERX|115=LSETR|34=2006|52=20120126-15:15:54|370=20120126-15:15:54.822|571=124|150=0|939=0|10=059|',
'outgoing: 8=FIX.4.2|9=130|35=AE|49=LSEHub|56=LSETR|115=BROKERX|34=2287|43=N|52=20120330-12:14:09|370=20120330-12:14:09.816|571=00008661533TRLO1-1-1-0|150=H|10=074|\n' +
'incoming: 8=FIX.4.2|9=109|35=AR|34=2486|49=LSETR|52=20120330-12:14:10.379|56=LSEHub|128=BROKERX|150=H|571=00008661533TRLO1-1-1-0|939=0|10=073|',
'outgoing: 8=FIX.4.2|9=175|35=AE|49=BROKERX|56=LSEHub|34=17|52=20120202-16:04:44|128=LSETR|918=EUR|31=89.0000000|64=20120202|828=1007|60=20120202-16:04:44|32=6|22=4|571=1698|570=N|150=0|48=AT0000785555|10=076|\n' +
'incoming: 8=FIX.4.2|9=111|35=AR|49=LSEHub|56=BROKERX|115=LSETR|34=16|52=20120202-16:04:44|370=20120202-16:04:45.257|571=1698|150=0|939=0|10=015|'
];

$('a.sample-message-link').click(function()
Expand Down
2 changes: 2 additions & 0 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ tr.system-field
color: #aaa;
}

tr.system-field.valid .field-value,
tr.header-field.valid .field-value
{
color: #0c0;
}

tr.system-field.invalid .field-value,
tr.header-field.invalid .field-value
{
color: #f00;
Expand Down

0 comments on commit d079784

Please sign in to comment.