diff --git a/CHANGELOG.md b/CHANGELOG.md index 1862b8013..49cdd5bb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format. - `Faker.Gov.It` [[@neslinesli93](https://github.com/neslinesli93)] - `Faker.Vehicle.model/1` [[@daytonn](https://github.com/daytonn)] - `Faker.Person`[[@anthonator](https://github.com/anthonator)] +- `Faker.String.hexadecimal/0` and `Faker.String.hexadecimal/1`[[@ilyashuma](https://github.com/ilyashuma)] ### Changed - Fix `Faker.Code.Iban.iban` and `Faker.Gov.It.fiscal_id` doctests [[@vbrazo](https://github.com/vbrazo)] diff --git a/lib/faker/string.ex b/lib/faker/string.ex index 0c861e09a..f077146dc 100644 --- a/lib/faker/string.ex +++ b/lib/faker/string.ex @@ -24,4 +24,24 @@ defmodule Faker.String do |> Base.encode64() |> binary_part(0, length) end + + @doc """ + Returns a random hexadecimal String + ## Examples + iex> Faker.String.hexadecimal() + "d6d98b88c866f496dbd4de7ba48d0f52" + iex> Faker.String.hexadecimal() + "c9edd02cc9f381def9d922a146bf8550" + iex> Faker.String.hexadecimal(5) + "9b5c2" + iex> Faker.String.hexadecimal(100) + "9a3a4993ad56b778dc26eddb7e0c2e49c4e638e62de32933bc3525bb4594a1a378dc29f741dd703efd94dd3b6d08feaa53a9" + """ + @spec hexadecimal(pos_integer) :: String.t() + def hexadecimal(length \\ 32) do + length + |> Faker.random_bytes() + |> Base.encode16(case: :lower) + |> binary_part(0, length) + end end