๐Ÿงฉ Hashing Guide

July 25, 2025 ยท View on GitHub

Lua API Difficulty Version

๐Ÿš€ Quick Start Guide for hashing with LuaDoTheWorld


๐Ÿ“‹ What You'll Learn

  • โœ… How to create a SHA256 hash from a string
  • โœ… How to create a SHA256 hash from a file
  • โœ… How to create a SHA256 hash from a folder (by content or by last modification)
  • โœ… How to use the Hasher object to combine hashes

๐Ÿ› ๏ธ Prerequisites

  • LuaDoTheWorld installed and required in your script

๐Ÿ”‘ Hash a String

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local sha = dtw.generate_sha("hello world")
print(sha)

๐Ÿ“„ Hash a File

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local sha = dtw.generate_sha_from_file("tests/target/blob.png")
print(sha)

๐Ÿ“ Hash a Folder by Content

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local sha = dtw.generate_sha_from_folder_by_content("tests/target/test_dir")
print(sha)

๐Ÿ•’ Hash a Folder by Last Modification

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local sha = dtw.generate_sha_from_folder_by_last_modification("tests/target/test_dir")
print(sha)

๐Ÿ”„ Hash a Lua Table or Variable

You can hash a Lua table or variable directly using dtw.digest_var. This is useful for generating a SHA256 hash from structured data (tables, arrays, etc.).

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local data = dtw.digest_var({name="Mateus", age=38, hobbies={"coding", "gaming"}})
print("data:", data) -- 13d17b23ccee37a92107f8357e980851c6b05fd6e0a7f9878dc384babc8858e4

๐Ÿงฉ Combine Hashes with Hasher

If you want to hash many things together, use the Hasher object:

local dtw = require("luaDoTheWorld/luaDoTheWorld")
local hasher = dtw.newHasher()
hasher.digest_file("tests/target/blob.png")
hasher.digest("hello world")
hasher.digest_folder_by_content("tests/target/test_dir")
print(hasher.get_value())

๐Ÿ“š Quick Reference

FunctionWhat it doesExample
dtw.generate_sha(str)Hash a stringdtw.generate_sha("hello")
dtw.generate_sha_from_file(path)Hash a filedtw.generate_sha_from_file("file.png")
dtw.generate_sha_from_folder_by_content(path)Hash folder by contentdtw.generate_sha_from_folder_by_content("dir")
dtw.generate_sha_from_folder_by_last_modification(path)Hash folder by last modificationdtw.generate_sha_from_folder_by_last_modification("dir")
dtw.digest_var(table)Hash a Lua table/variabledtw.digest_var({a=1, b=2})
dtw.newHasher()Create a hasher objectdtw.newHasher()
hasher.digest(data)Add string to hasherhasher.digest("hello")
hasher.digest_file(path)Add file to hasherhasher.digest_file("file.png")
hasher.digest_folder_by_content(path)Add folder to hasherhasher.digest_folder_by_content("dir")
hasher.get_value()Get the final hashhasher.get_value()

๐Ÿ†˜ Need Help?

  • ๐Ÿ“– Check the main SDK documentation
  • ๐Ÿ” Look at other example scripts in the SDK
  • ๐Ÿ› Report issues on our GitHub repository

Footer