Implement algorithm switching
This commit implements every algorithm I have played with so far. It also allows for you to switch which algorithm you want to use at runtime.
This commit is contained in:
@@ -1,34 +1,58 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
import { config } from '~~/server/utils/config';
|
||||
import { generate_challenge } from '@impost/lib/validator';
|
||||
import { ChallengeStrategy } from '@impost/lib';
|
||||
import { generate_challenge, kCTFChallengeConfig, Argon2ChallengeConfig, SHA256ChallengeConfig } from '@impost/lib/validator';
|
||||
import { ChallengeStrategy, ChallengeAlgorithm } from '@impost/lib';
|
||||
import { CHALLENGE_TIMEOUT_MS, outstandingChallenges } from '~~/server/utils/pow';
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
let challenge_config;
|
||||
// switch (config.strategy) {
|
||||
// case ChallengeStrategy.LeadingZeroes:
|
||||
// challenge_config = {
|
||||
// parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
||||
// strategy: config.strategy,
|
||||
// difficulty: config.leading_zeroes?.difficulty!,
|
||||
// };
|
||||
// break;
|
||||
// case ChallengeStrategy.TargetNumber:
|
||||
// challenge_config = {
|
||||
// parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
||||
// strategy: config.strategy,
|
||||
// max_number: config.target_number.max_number,
|
||||
// };
|
||||
// break;
|
||||
// }
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.kCTF:
|
||||
switch (config.algorithm) {
|
||||
case ChallengeAlgorithm.SHA256:
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.SHA256,
|
||||
strategy: ChallengeStrategy.LeadingZeroes,
|
||||
difficulty: config.leading_zeroes.difficulty,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
} as SHA256ChallengeConfig;
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.SHA256,
|
||||
strategy: ChallengeStrategy.TargetNumber,
|
||||
difficulty: config.target_number.max_number,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
} as SHA256ChallengeConfig;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeAlgorithm.Argon2id:
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.Argon2id,
|
||||
strategy: ChallengeStrategy.LeadingZeroes,
|
||||
difficulty: config.leading_zeroes.difficulty,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
} as Argon2ChallengeConfig;
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
challenge_config = {
|
||||
algorithm: ChallengeAlgorithm.Argon2id,
|
||||
strategy: ChallengeStrategy.TargetNumber,
|
||||
difficulty: config.target_number.max_number,
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
} as Argon2ChallengeConfig;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ChallengeAlgorithm.kCTF:
|
||||
challenge_config = {
|
||||
parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
||||
strategy: config.strategy,
|
||||
algorithm: ChallengeAlgorithm.kCTF,
|
||||
difficulty: config.kctf.difficulty,
|
||||
};
|
||||
parameters: { expires_at: Date.now() + CHALLENGE_TIMEOUT_MS },
|
||||
} as kCTFChallengeConfig;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -40,10 +64,10 @@ export default defineEventHandler(async () => {
|
||||
});
|
||||
}
|
||||
|
||||
outstandingChallenges.set(challenge.challenge, {
|
||||
outstandingChallenges.set(challenge.salt, {
|
||||
challenge, timeout: setTimeout(() => {
|
||||
console.log("Challenge timed out:", challenge.challenge);
|
||||
outstandingChallenges.delete(challenge.challenge);
|
||||
console.log("Challenge timed out:", challenge.salt);
|
||||
outstandingChallenges.delete(challenge.salt);
|
||||
}, CHALLENGE_TIMEOUT_MS)
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user