joshmfrankel / joshmfrankel.github.io

Blog and Personal site
http://joshfrankel.me
MIT License
2 stars 1 forks source link

How to utilize HMAC sha256 for a postgres column with set credentials in Rails #80

Open joshmfrankel opened 7 months ago

joshmfrankel commented 7 months ago
  def up
    hmac_key = Rails.application.credentials.some_name[:key]
    safety_assured do
      execute <<~SQL
        UPDATE table
        SET column_name = 'prefix-'::text || encode(hmac(team_id::text, '#{hmac_key}', 'sha256'), 'hex') || '-suffix'::text
      SQL
    end
  end

encode ensures that escaped characters such as \\x are removed from the resulting HMAC.