Reputation: 493
I'm sending an email with an attachment from one server to another server and I would like to find a good way to verify that this email really comes from that server.
The applications are in PHP. I can't verify the origin with for example the IP (server in an Intranet). Could I use a hash in subject or in the body of the email.
Upvotes: 0
Views: 38
Reputation: 4503
Generating a hash alone is not enough. What you need is asymmetric cryptography. The first step to implementing this would be to generate a public/private key pair. This is easily accomplished using openssl.
openssl genrsa -out private.key 1024
openssl rsa -in private.key -pubout > public.key
For example:
<?php
$hash = hash_file("md5", "path/to/your.file");
$key = openssl_pkey_get_private("file://path/to/your/private.key");
openssl_sign($hash, $signature, $key);
openssl_free_key($key);
// build your message and attach the file
$headers['X-Signature'] = base64_encode($signature);
mail($to, $subject, $message, $headers);
?>
Something like this:
<?php
require_once('email-parser.php');
$msg = parse_email_file("path/to/your/message.eml");
$signature = base64_decode($msg['headers']['X-Signature']);
$hash = md5($msg['attachment']);
$key = openssl_pkey_get_public("file://path/to/your/public.key");
$verified = openssl_verify($hash, $signature, $key);
openssl_free_key($key);
if ($verified) {
// DO STUFF
} else {
// PANIC!!!
}
?>
Upvotes: 1