Here is a solution that I have tested several times and it seems to work. I'm sure someone with more experience can offer a solution which is much quicker than mine, but so far this is the best I can come up with.
I strongly suggest making a copy of your "recovered" files with generic file naming. Then I would run the following code on your copied files.
This AppleScript code works for me using the latest version of macOS Mojave.
Just paste this AppleScript code into Script Editor.app and you can run the code from there.
set genericFolder to (choose folder with prompt "Choose The Parent Folder Containing The Files To Be Renamed")
set sourceFolder to (choose folder with prompt "Choose The Parent Folder Containing The Files With Proper Names")
tell application "Finder"
set genericFiles to files of entire contents of genericFolder as alias list
set sourceFiles to files of entire contents of sourceFolder as alias list
end tell
repeat with genericFile in genericFiles
repeat with sourceFile in sourceFiles
try
set isIdentical to (last word of (do shell script "diff -s " & quoted form of POSIX path of genericFile & " " & quoted form of POSIX path of sourceFile) is "identical") as boolean
if isIdentical is true then
tell application "System Events" to set name of genericFile to name of sourceFile
end if
end try
end repeat
end repeat
This image is the folder and file structure before running the code. The source files are on the left image and the files to be renamed are on the right image. Take notice that the folder and file structures are different in both images.
This next image is the result after running the code
This solution was tested with only 38 files with generic names. On my system it took 1 minute and 33 seconds to process and rename the files accordingly. Without doing any actual math, if you are trying to process 500,000 files with this code, you could be looking at the better part of a full day for the process to complete. You may want to consider breaking the process down to processing a few folders at time, rather than the full mother load all at once.
However, if you are only looking to rename files with the name extension ".doc" , you can replace this code from above...
tell application "Finder"
set genericFiles to files of entire contents of genericFolder as alias list
set sourceFiles to files of entire contents of sourceFolder as alias list
end tell
With this code instead, which will make the process much much quicker.
tell application "Finder"
set genericFiles to ((files of entire contents of genericFolder) whose name extension is "doc") as alias list
set sourceFiles to ((files of entire contents of sourceFolder) whose name extension is "doc") as alias list
end tell
You can also have the code process only files with name extensions that you define, as in this following example.
set nameExtensions to {"doc", "pdf"}
tell application "Finder"
set genericFiles to ((files of entire contents of genericFolder) whose name extension is in nameExtensions) as alias list
set sourceFiles to ((files of entire contents of sourceFolder) whose name extension is in nameExtensions) as alias list
end tell