gradido/login_server/src/cpsp/DebugMnemonic.cpsp
2021-05-20 13:45:47 +02:00

136 lines
3.9 KiB
Plaintext

<%@ page class="DebugMnemonicPage" %>
<%@ page form="true" %>
<%@ page baseClass="SessionHTTPRequestHandler" %>
<%@ page ctorArg="Session*" %>
<%@ header include="HTTPInterface/SessionHTTPRequestHandler.h" %>
<%!
#include "ServerConfig.h"
#include "Crypto/Passphrase.h"
struct WordChecked {
WordChecked() : index(0), bSet(false) {};
int index;
std::string word;
std::string language;
bool bSet;
std::string print()
{
std::string str;
str = std::to_string(index);
str += ": ";
str += word;
str += " (";
str += language;
str += ")";
return str;
}
};
const char* getLanguageByMnemonicListIndex(ServerConfig::Mnemonic_Types type)
{
switch(type) {
case ServerConfig::MNEMONIC_GRADIDO_BOOK_GERMAN_RANDOM_ORDER: return "de";
case ServerConfig::MNEMONIC_GRADIDO_BOOK_GERMAN_RANDOM_ORDER_FIXED_CASES: return "de";
case ServerConfig::MNEMONIC_BIP0039_SORTED_ORDER: return "en";
}
return "unknown";
}
%>
<%%
const char* pageName = "Debug Mnemonic";
WordChecked checkedWord;
WordChecked checkedIndex[ServerConfig::Mnemonic_Types::MNEMONIC_MAX];
if(!form.empty())
{
if("" != form.get("check_word", ""))
{
auto word = Passphrase::filter(form.get("word", ""));
if("" != word) {
checkedWord.bSet = true;
checkedWord.word = word;
for (int i = ServerConfig::MNEMONIC_GRADIDO_BOOK_GERMAN_RANDOM_ORDER; i < ServerConfig::Mnemonic_Types::MNEMONIC_MAX; i++)
{
Mnemonic& m = ServerConfig::g_Mnemonic_WordLists[i];
if (word != "\0" && word != "" && word.size() > 3) {
if(m.isWordExist(word)) {
checkedWord.index = m.getWordIndex(word.data());
checkedWord.language = getLanguageByMnemonicListIndex((ServerConfig::Mnemonic_Types)i);
break;
}
}
else {
addError(new Error("Word", "Ungültiges Wort, es sollte länger als 3 Zeichen sein"));
checkedWord.bSet = false;
break;
}
}
}
}
if("" != form.get("check_index", ""))
{
try {
auto index = stoi(form.get("index", ""));
if(index < 0 || index >= 2048) {
addError(new Error("Index", "Ung&uuml;ltiger Index, muss sich im Bereich [0:2047] bewegen"));
} else {
for (int i = ServerConfig::MNEMONIC_GRADIDO_BOOK_GERMAN_RANDOM_ORDER; i < ServerConfig::Mnemonic_Types::MNEMONIC_MAX; i++)
{
Mnemonic& m = ServerConfig::g_Mnemonic_WordLists[i];
checkedIndex[i].bSet = true;
checkedIndex[i].index = index;
checkedIndex[i].word = m.getWord(index);
checkedIndex[i].language = getLanguageByMnemonicListIndex((ServerConfig::Mnemonic_Types)i);
}
}
} catch(...) {
addError(new Error("Index", "Ung&uuml;ltiger Index, keine Nummer"));
}
}
}
%><%@ include file="include/header_old.cpsp" %>
<div class="grd_container">
<h1>Debug Mnemonic</h1>
<%= getErrorsHtml() %>
<form method="POST">
<fieldset class="grd_container_small">
<legend>Wort prüfen</legend>
<p class="grd_small">
<label for="word">Word</label>
<input id="word" type="text" name="word" value="<%= form.get("word", "") %>"/>
</p>
<input type="submit" name="check_word" value="Wort &uuml;berpr&uuml;fen"/>
<% if(checkedWord.bSet) { %>
<p><%= checkedWord.print() %></p>
<% } %>
</fieldset>
<fieldset class="grd_container_small">
<legend>Index prüfen</legend>
<p class="grd_small">
<label for="index">Index</label>
<input id="index" type="text" name="index" value="<%= form.get("index", "") %>"/>
</p>
<input type="submit" name="check_index" value="Index &uuml;berpr&uuml;fen"/>
<% if(checkedIndex[0].bSet) { %>
<ul class="grd-no-style">
<% for (int i = ServerConfig::MNEMONIC_GRADIDO_BOOK_GERMAN_RANDOM_ORDER; i < ServerConfig::Mnemonic_Types::MNEMONIC_MAX; i++) { %>
<li>
<%= checkedIndex[i].print() %>
</li>
<% } %>
</ul>
<% } %>
</fieldset>
</form>
</div>
<%@ include file="include/footer.cpsp" %>