Skip to content

Commit f15374a

Browse files
committed
improved hash verification
1 parent 708bccc commit f15374a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/app/src/app/race/actions.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,21 @@ export const saveUserResultAction = validatedCallback({
6969

7070
// verify hash:
7171
const uniqueKey = 'your-unique-key';
72-
const hashedData = CryptoJS.HmacSHA256(input.data, uniqueKey).toString();
72+
const data = {
73+
timeTaken: input.timeTaken,
74+
errors: input.errors,
75+
cpm: input.cpm,
76+
accuracy: input.accuracy,
77+
snippetId: input.snippetId,
78+
};
79+
const jsonData = JSON.stringify(data);
80+
const hashedData = CryptoJS.HmacSHA256(jsonData, uniqueKey).toString();
7381

7482
if( hashedData != input.hash.toString() ){
75-
console.log(input.hash.toString(), hashedData);
76-
throw new Error("Invalid Request: Data tampered");
83+
console.log(jsonData, input.hash.toString(), hashedData);
84+
throw new Error("Invalid Request: Tampered Data");
7785
}
86+
else console.log("Nice");
7887

7988
return await prisma.$transaction(async (tx) => {
8089
const result = await tx.result.create({

0 commit comments

Comments
 (0)