summaryrefslogtreecommitdiff
path: root/files/cardconv.html
blob: cb0f0819bc994617addf0a6b14191cbb92858862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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:&nbsp;<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>