We introduce twisted Steinberg algebras over a commutative unital ring $R$. These generalise Steinberg algebras and are a purely algebraic analogue of Renault's twisted groupoid C*-algebras. In particular, for each ample Hausdorff groupoid $G$ and each locally constant $2$-cocycle $\sigma$ on $G$ taking values in the units $R^\times$, we study the algebra $A_R(G,\sigma)$ consisting of locally constant compactly supported $R$-valued functions on $G$, with convolution and involution "twisted" by $\sigma$. We also introduce a "discretised" analogue of a twist $\Sigma$ over a Hausdorff \'etale groupoid $G$, and we show that there is a one-to-one correspondence between locally constant $2$-cocycles on $G$ and discrete twists over $G$ admitting a continuous global section. Given a discrete twist $\Sigma$ arising from a locally constant $2$-cocycle $\sigma$ on an ample Hausdorff groupoid $G$, we construct an associated twisted Steinberg algebra $A_R(G;\Sigma)$, and we show that it coincides with $A_R(G,\sigma^{-1})$. Given any discrete field $\mathbb{F}_d$, we prove a graded uniqueness theorem for $A_{\mathbb{F}_d}(G,\sigma)$, and under the additional hypothesis that $G$ is effective, we prove a Cuntz--Krieger uniqueness theorem and show that simplicity of $A_{\mathbb{F}_d}(G,\sigma)$ is equivalent to minimality of $G$.