Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Networking;
- using System.Collections;
- using System.IO;
- using System.Security.Cryptography;
- public class ServerFileValidation : MonoBehaviour
- {
- private string serverURL = "https://yourserver.com/validateFile";
- void Start()
- {
- string filePath = Application.dataPath + "/YourImportantFile.txt";
- StartCoroutine(ValidateFileWithServer(filePath));
- }
- IEnumerator ValidateFileWithServer(string filePath)
- {
- // Compute the file's SHA-256 hash
- string fileHash = ComputeFileHash(filePath);
- if (fileHash != null)
- {
- // Send the file hash to the server for validation
- UnityWebRequest www = UnityWebRequest.Get(serverURL + "?fileHash=" + fileHash);
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.LogError("Error connecting to server: " + www.error);
- }
- else
- {
- // Process the server's response
- if (www.downloadHandler.text != "OK")
- {
- Debug.LogError("File validation failed. Exiting game.");
- Application.Quit();
- }
- else
- {
- Debug.Log("File validation successful.");
- }
- }
- }
- }
- string ComputeFileHash(string filePath)
- {
- try
- {
- using (SHA256 sha256 = SHA256.Create())
- {
- using (FileStream fileStream = File.OpenRead(filePath))
- {
- byte[] fileHash = sha256.ComputeHash(fileStream);
- return ByteArrayToString(fileHash);
- }
- }
- }
- catch (System.Exception e)
- {
- Debug.LogError("Error computing file hash: " + e.Message);
- return null;
- }
- }
- string ByteArrayToString(byte[] byteArray)
- {
- return System.BitConverter.ToString(byteArray).Replace("-", "").ToLower();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement