Sixth seed Nadal, bidding for a men's record 21st Grand Slam title, beat Germany's Hanfmann 6-2 6-3 6-4.
Hanfmann saved four match points, including two on the Spaniard's serve, before the 2009 champion secured victory on Rod Laver Arena.
Alexander Zverev also reached the third round after beating Australia's John Millman in straight sets.
Despite a spirited performance from the Australian, the German third seed won 6-4 6-4 6-0 to set up a meeting with Moldovan qualifier Radu Albot, who beat wildcard Aleksandar Vukic 6-4 7-6 (7-4) 6-4.