{% extends 'base.html' %}
{% block content %}
<div class="container-fluid">
<h1 class="mt-4">포렌식</h1>
<p></p>
<hr>
  <!-- 탭 헤더 -->
  <ul class="nav nav-tabs" id="myTab" role="tablist">
  
    <li class="nav-item" role="presentation">
      <a class="nav-link {% if active_tab == 'home' %}active{% endif %}" id="home-tab" data-bs-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="{% if active_tab == 'home' %}true{% else %}false{% endif %}">해시</a>
    </li>
	
    <li class="nav-item" role="presentation">
      <a class="nav-link {% if active_tab == 'menu1' %}active{% endif %}" id="menu1-tab" data-bs-toggle="tab" href="#menu1" role="tab" aria-controls="menu1" aria-selected="{% if active_tab == 'menu1' %}true{% else %}false{% endif %}">메타정보</a>
    </li>
	
    <li class="nav-item" role="presentation">
      <a class="nav-link {% if active_tab == 'menu2' %}active{% endif %}" id="menu2-tab" data-bs-toggle="tab" href="#menu2" role="tab" aria-controls="menu2" aria-selected="{% if active_tab == 'menu2' %}true{% else %}false{% endif %}">암호</a>
    </li>	
	
    <li class="nav-item" role="presentation">
      <a class="nav-link {% if active_tab == 'menu3' %}active{% endif %}" id="menu3-tab" data-bs-toggle="tab" href="#menu3" role="tab" aria-controls="menu3" aria-selected="{% if active_tab == 'menu3' %}true{% else %}false{% endif %}">레지스트리</a>
    </li>    
	
	
  </ul>
<!-- 탭 콘텐츠 -->
  <div class="tab-content" id="myTabContent">
  
    <div class="tab-pane fade {% if active_tab == 'home' %}show active{% endif %}" id="home" role="tabpanel" aria-labelledby="home-tab">
      <p></p>

             <div class="card">
                <div class="card-body">
                    해시란?<br>
                    임의의 길이를 가진 데이터를 <strong>고정된 길이의 값으로 변환하는 함수나 결과</strong>를 말함. 이때 결과값을 해시값이라고 부른다.<br>    
                </div>
            </div>
            <br>       
실습
<form action="" method="POST" enctype="multipart/form-data">
  <input type="hidden" name="part" value="encoding">
  <fieldset class="mt-3">

    <!-- 라디오 버튼 -->
    <div class="mb-3">
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="convert_select" id="radioString" value="string" onclick="toggleInput('string')">
        <label class="form-check-label" for="radioString">문자열</label>
      </div>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="convert_select" id="radioFile" value="file" onclick="toggleInput('file')">
        <label class="form-check-label" for="radioFile">파일</label>
      </div>
    </div>

    <!-- 문자열 입력 -->
    <div id="stringInput" class="mb-3" style="display:none;">
      <label for="inputText" class="form-label">문자열 입력</label>
      <input type="text" class="form-control" id="inputText" name="inputText" placeholder="문자열을 입력하세요">
    </div>

    <!-- 파일 업로드 -->
    <div id="fileInput" class="mb-3" style="display:none;">
      <label for="inputFile" class="form-label">파일 업로드</label>
      <input class="form-control" type="file" id="inputFile" name="inputFile">
    </div>

    <!-- 전송 버튼 -->
    <button type="submit" name="button" class="btn btn-primary">변환</button>

  </fieldset>
</form>

<script>
  function toggleInput(type) {
    const stringDiv = document.getElementById('stringInput');
    const fileDiv = document.getElementById('fileInput');

    if (type === 'string') {
      stringDiv.style.display = 'block';
      fileDiv.style.display = 'none';
    } else if (type === 'file') {
      stringDiv.style.display = 'none';
      fileDiv.style.display = 'block';
    }
  }
</script>
<hr>
변환결과:<br>
<div id="result"><b>{{convert_text}}</b> : {{convert_result}}</div>
  
               <div class="card">
                <div class="card-body">
                  <code>
                    def	calculate_hash_string(text,	algorithm="md5"):<br>
	  &nbsp;&nbsp;"""문자열 해시 계산"""<br>
	  &nbsp;&nbsp;h =	hashlib.new(algorithm)<br>
	  &nbsp;&nbsp;h.update(text.encode("utf-8"))<br>
	  &nbsp;&nbsp;return h.hexdigest()<br>

def	calculate_hash_file(file, algorithm="md5"):<br>
	  &nbsp;&nbsp;"""파일 해시 계산	(chunk 단위로)"""<br>
	  &nbsp;&nbsp;h =	hashlib.new(algorithm)<br>
	  &nbsp;&nbsp;while chunk	:= file.read(8192):<br>
		&nbsp;&nbsp;  h.update(chunk)<br>
	  &nbsp;&nbsp;file.seek(0)  #	파일 포인터 초기화 (다시 사용 가능하도록)<br>
	  &nbsp;&nbsp;return h.hexdigest()<br>

<hr>
if option == "string":<br>
				&nbsp;&nbsp;input_text = request.form.get("inputText", "")<br>
				&nbsp;&nbsp;if input_text:<br>
				&nbsp;&nbsp;	result = calculate_hash_string(input_text)<br>
				&nbsp;&nbsp;	return render_template('forensic/index.html',convert_text=input_text,convert_result=result,active_tab=active_tab)<br>
			elif option	== "file":<br>
			&nbsp;&nbsp;	uploaded_file =	request.files.get("inputFile")<br>
			&nbsp;&nbsp;	if uploaded_file and uploaded_file.filename	!= "":<br>
			&nbsp;&nbsp;		result = calculate_hash_file(uploaded_file)<br>
			&nbsp;&nbsp;		return render_template('forensic/index.html',convert_text=uploaded_file.filename,convert_result=result,active_tab=active_tab)<br>

                  </code>    
                </div>
            </div>
  </div>
	
	


    <div class="tab-pane fade {% if active_tab == 'menu1' %}show active{% endif %}" id="menu1" role="tabpanel" aria-labelledby="menu1-tab">
      <p></p>
             <div class="card">
                <div class="card-body">
                    메타정보란<br>
                    어떤 데이터의 성질, 구조, 작성자, 생성 시각, 크기, 형식 등을 설명하는 부가적인 정보<br>    
                </div>
            </div>
            <br>       
        <center>
        <img src="{{ url_for('static',filename='img/meta1.jpg') }}" style="width: 800px; height: 600px;">
      </center>
 <br>
      <form action="" method="POST" enctype="multipart/form-data">
		  <input type="hidden" name="active_tab" value="menu1">        
      <button type="submit" name="button" class="btn btn-primary">메타분석</button>
      </form>        
        분석결과:<br>
		<div id="result">{{convert_result}}</div>
  </div>
 
  


    <div class="tab-pane fade {% if active_tab == 'menu2' %}show active{% endif %}" id="menu2" role="tabpanel" aria-labelledby="menu2-tab">
      <p>암호</p>
        <form action="" method="POST">
		<input type="hidden" name="part" value="encoding">
		<input type="hidden" name="active_tab" value="menu2">
            <fieldset>
				<input type="radio" name="convert_select" value="encode">인코딩
				<input type="radio" name="convert_select" value="decode">디코딩		
                <input type="text" id="inputText" name="inputText">
                <input type="submit" name="button" value="변환">
            </fieldset>
        </form>
        <hr>
        변환결과:<br>
		<div id="result">{{convert_result}}</div>
  </div> 
  
  
  <div class="tab-pane fade {% if active_tab == 'menu3' %}show active{% endif %}" id="menu3" role="tabpanel" aria-labelledby="menu3-tab">
      <p></p>
        <div class="card">
          <div class="card-body">
            레지스트리란?<br>
                    <br>    
          </div>
        </div>
    <table class="table table-hover">
    <thead>
      <tr>
        <th>번호</th>
        <th>Hive</th>
        <th>위치</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>      

  </div>  



  

  


  

</div>
{% endblock %}
