diff options
Diffstat (limited to 'files/cardconv.html')
-rw-r--r-- | files/cardconv.html | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/files/cardconv.html b/files/cardconv.html new file mode 100644 index 0000000..cb0f081 --- /dev/null +++ b/files/cardconv.html @@ -0,0 +1,197 @@ +<html> + <head> + + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> + </head> + <body> + + <div class="component m-3"> + <div class="row"> + <div class="col-sm mb-3"> + <div class="card"> + <div class="card-header"> + Kort info + </div> + <div class="card-body"> + <div class="input-group mb-3"> + <div class="input-group-prepend"> + <span class="input-group-text">ID</span> + </div> + <input style="font-family: monospace" type="text" id="cardid" class="form-control"> + <div class="input-group-append"> + <span style="font-family: monospace" id="checkdigitholder" class="input-group-text">check: <span id="checkdigit">00</span></span> + </div> + <div class="invalid-feedback"> + Kort id kan kun blive et tal med 6 cifre + </div> + </div> + <div id="errchkdigit" style="display: none" class="alert alert-danger"> + Invalid check digit + </div> + <button type="button" onclick="updatefromid()" class="btn btn-outline-secondary mb-3">Recalc</button> + </div> + </div> + </div> + <div class="col-sm"> + <div class="card"> + <div class="card-header"> + Sektor 15 + </div> + <div class="card-body"> + <p>Indholdet af block 2 i sektor 15</p> + <div class="input-group mb-3"> + <div class="input-group-prepend"> + <span class="input-group-text">Hex</span> + </div> + <textarea type="text" class="form-control" id="datahex" style="font-family: monospace"></textarea> + </div> + <div class="input-group"> + <div class="input-group-prepend"> + <span class="input-group-text">Ascii</span> + </div> + <textarea type="text" class="form-control" id="dataascii" style="font-family: monospace"></textarea> + <div class="invalid-feedback"> + Kunne ikke parse block 2 + </div> + </div> + </div> + </div> + </div> + </div> + </div> + + <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> + <script> + // Stolen from https://stackoverflow.com/questions/33920230/how-to-convert-string-from-ascii-to-hexadecimal-in-javascript-or-jquery + String.prototype.convertToHex = function (delim) { + return this.split("").map(function(c) { + return ("0" + c.charCodeAt(0).toString(16)).slice(-2); + }).join(delim || ""); + }; + +String.prototype.convertFromHex = function (delim) { + var str = ''; + for(var i = 0; i < this.length; i += 2) { + var v = parseInt(this.substr(i, 2), 16); + // Check if it could decode it + if( isNaN(v) ) { + return null; + } + + str += String.fromCharCode(v); + } + return str; +}; + +function calcsum(num) { + // Calculate the sum + var sum = 0; + while(num) { + sum += num % 10; + num = Math.floor(num / 10); + } + + // Subtract one and modulo 16 + return (sum-1) % 16; +} + +function resetcolours() { + $("#errchkdigit").hide(); + $("#checkdigitholder").removeClass("text-danger"); + $("#cardid").removeClass("is-invalid is-valid"); + $("#dataascii").removeClass("is-invalid is-valid"); + $("#datahex").removeClass("is-invalid is-valid"); +} + +function fillhex(blk) { + // Convert to hex + let blkhex = blk.convertToHex(); + $("#datahex").val(blkhex); + $("#datahex").addClass("is-valid"); + +} + +function updatefromid() { + resetcolours(); + var id = parseInt($("#cardid").val()); + if( isNaN(id) || id.toString().length > 6 || id < 0) { + $("#cardid").addClass("is-invalid"); + return + } + + // Update check digit + var chk = calcsum(id) + $("#checkdigit").text(chk.toString().padStart(2, '0')); + + // Generate block 2 content and save it + var paddedid = id.toString().padEnd(6, '0'); + var chkascii = String.fromCharCode(0x30 + chk); + var blkascii = `000${paddedid}0?${chkascii}0000` + $("#dataascii").val(blkascii) + $("#dataascii").addClass("is-valid"); + + fillhex(blkascii); + +} + +function updatefromascii(updatehex) { + if( updatehex ) { + resetcolours(); + } + let match_regex = /000([0-9]{6})0\?([0-9:;<=>?])0000/ + + // Extact values + console.log("0001200000?20000" === $("#dataascii").val()); + let tag = $("#dataascii").val().match(match_regex); + if( tag == null ) { + $("#dataascii").addClass("is-invalid"); + return + } + + // Extract vars + idstr = tag[1]; + chkascii = tag[2]; + + // Parse the ints, we assume id and chk are valid + id = parseInt(idstr); + chk = chkascii.charCodeAt(0) - 0x30; + + // Verify check digit + if( calcsum(id) != chk ) { + $("#errchkdigit").show(); + $("#checkdigitholder").addClass("text-danger"); + } + + // Fill in stuff everywhere else + $("#cardid").val(id); + $("#checkdigit").text(chk.toString().padStart(2, "0")); + // Mark as valid + $("#cardid").addClass("is-valid"); + + // This will be set to event by an eventhandler, but false if called from another updatefromhex + if( updatehex ) { + fillhex($("#dataascii").val()); + } +} + +function updatefromhex() { + resetcolours(); + + var data = $("#datahex").val().convertFromHex(); + + $("#dataascii").val(data); + $("#dataascii").addClass("is-valid"); + updatefromascii(false); +} + +// Attach event +$("#cardid").on('input', updatefromid); +$("#dataascii").on('input', updatefromascii); +$("#datahex").on('input', updatefromhex); +// Run to update all the fields +updatefromid(); + </script> + + </body> +</html> |