シグネチャとは、電子メールなどのメッセージの末尾に付加されるメッセージや個人情報のことである。
シグネチャは、メールソフトの機能として提供されていることが多く、あらかじめ記述する内容を設定しておくことで、送信メールにかならずシグネチャが付加されるようになる。なお、データやメッセージの正当性を補償する電子署名とは別のものである。
シグネチャの用途としては、例えばビジネスでは、送信者の会社の連絡先や部署、氏名などを記すのが一般的であるが、プライベートで用いる場合は、送信者のメッセージなどが記されていることもある。
構文
解説Signature クラスは、XML デジタル署名仕様に定義されている XML 署名の <Signature> 要素を表します。<Signature> 要素は、XML デジタル署名のルート要素です。SignedInfo、SignatureValue、KeyInfo、および ObjectList プロパティは、<Signature> 要素のサブ要素をカプセル化します。
<Signature> 要素の詳細については、www.w3.org/TR/xmldsig-core/ の W3C 仕様を参照してください。
使用例Signature クラスを SignedXml クラスと共に使用し、エンベロープ シグネチャで XML ドキュメントの署名および検証を行うコードの例を次に示します。
' This example signs an XML file using an ' envelope signature. It then verifies the ' signed XML. ' Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Text Imports System.Xml Module SignVerifyEnvelope Sub Main(ByVal args() As String) ' Generate a signing key. Dim Key As New RSACryptoServiceProvider() Try ' Sign an XML file and save the signature to a ' new file. SignXmlFile("Test.xml", "SignedExample.xml", Key) Console.WriteLine("XML file signed.") ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") Dim result As Boolean = VerifyXmlFile("SignedExample.xml") ' Display the results of the signature verification to ' the console. If result Then Console.WriteLine("The XML signature is valid.") Else Console.WriteLine("The XML signature is not valid.") End If Catch e As CryptographicException Console.WriteLine(e.Message) Finally ' Clear resources associated with the ' RSACryptoServiceProvider. Key.Clear() End Try End Sub ' Sign an XML file and save the signature in a new file. Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal Key As RSA) ' Check the arguments. If FileName Is Nothing Then Throw New ArgumentNullException("FileName") End If If SignedFileName Is Nothing Then Throw New ArgumentNullException("SignedFileName") End If If Key Is Nothing Then Throw New ArgumentNullException("Key") End If ' Create a new XML document. Dim doc As New XmlDocument() ' Format the document to ignore white spaces. doc.PreserveWhitespace = False ' Load the passed XML file using it's name. doc.Load(New XmlTextReader(FileName)) ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) ' Add the key to the SignedXml document. signedXml.SigningKey = Key ' Get the signature object from the SignedXml object. Dim XMLSignature As Signature = signedXml.Signature ' Create a reference to be signed. Pass "" ' to specify that all of the current XML ' document should be signed. Dim reference As New Reference("") ' Add an enveloped transformation to the reference. Dim env As New XmlDsigEnvelopedSignatureTransform() reference.AddTransform(env) ' Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference) ' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). Dim keyInfo As New KeyInfo() keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA))) ' Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo ' Compute the signature. signedXml.ComputeSignature() ' Get the XML representation of the signature and save ' it to an XmlElement object. Dim xmlDigitalSignature As XmlElement = signedXml.GetXml() ' Append the element to the XML document. doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True)) If TypeOf doc.FirstChild Is XmlDeclaration Then doc.RemoveChild(doc.FirstChild) End If ' Save the signed XML document to a file specified ' using the passed string. Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False)) doc.WriteTo(xmltw) xmltw.Close() End Sub ' Verify the signature of an XML file and return the result. Function VerifyXmlFile(ByVal Name As String) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Format using white spaces. xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. xmlDocument.Load(Name) ' Create a new SignedXml object and pass it ' the XML document class. Dim signedXml As New SignedXml(xmlDocument) ' Find the "Signature" node and create a new ' XmlNodeList object. Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") ' Load the signature node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. Return signedXml.CheckSignature() End Function End Module
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Text; using System.Xml; public class SignVerifyEnvelope { public static void Main(String[] args) { // Generate a signing key. RSACryptoServiceProvider Key = new RSACryptoServiceProvider(); try { // Sign an XML file and save the signature to a // new file. SignXmlFile("Test.xml", "SignedExample.xml", Key); Console.WriteLine("XML file signed."); // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification to // the console. if (result) { Console.WriteLine("The XML signature is valid."); } else { Console.WriteLine("The XML signature is not valid."); } } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key.Clear(); } } // Sign an XML file and save the signature in a new file. public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) { // Check the arguments. if (FileName == null) throw new ArgumentNullException("FileName"); if (SignedFileName == null) throw new ArgumentNullException("SignedFileName"); if (Key == null) throw new ArgumentNullException("Key"); // Create a new XML document. XmlDocument doc = new XmlDocument(); // Format the document to ignore white spaces. doc.PreserveWhitespace = false; // Load the passed XML file using it's name. doc.Load(new XmlTextReader(FileName)); // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); // Add the key to the SignedXml document. signedXml.SigningKey = Key; // Get the signature object from the SignedXml object. Signature XMLSignature = signedXml.Signature; // Create a reference to be signed. Pass "" // to specify that all of the current XML // document should be signed. Reference reference = new Reference(""); // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); // Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(new RSAKeyValue((RSA)Key)); // Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo; // Compute the signature. signedXml.ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml(); // Append the element to the XML document. doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); if (doc.FirstChild is XmlDeclaration) { doc.RemoveChild(doc.FirstChild); } // Save the signed XML document to a file specified // using the passed string. XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false)); doc.WriteTo(xmltw); xmltw.Close(); } // Verify the signature of an XML file and return the result. public static Boolean VerifyXmlFile(String Name) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Format using white spaces. xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument.Load(Name); // Create a new SignedXml object and pass it // the XML document class. SignedXml signedXml = new SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature"); // Load the signature node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return signedXml.CheckSignature(); } }
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // #using <System.Xml.dll> #using <System.Security.dll> #using <System.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; // Sign an XML file and save the signature in a new file. static void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key ) { // Check the arguments. if ( FileName == nullptr ) throw gcnew ArgumentNullException( L"FileName" ); if ( SignedFileName == nullptr ) throw gcnew ArgumentNullException( L"SignedFileName" ); if ( Key == nullptr ) throw gcnew ArgumentNullException( L"Key" ); // Create a new XML document. XmlDocument^ doc = gcnew XmlDocument; // Format the document to ignore white spaces. doc->PreserveWhitespace = false; // Load the passed XML file using it's name. doc->Load( gcnew XmlTextReader( FileName ) ); // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml( doc ); // Add the key to the SignedXml document. signedXml->SigningKey = Key; // Get the signature object from the SignedXml object. Signature^ XMLSignature = signedXml->Signature; // Create a reference to be signed. Pass "" // to specify that all of the current XML // document should be signed. Reference^ reference = gcnew Reference( L"" ); // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform^ env = gcnew XmlDsigEnvelopedSignatureTransform; reference->AddTransform( env ); // Add the Reference object to the Signature object. XMLSignature->SignedInfo->AddReference( reference ); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo; keyInfo->AddClause( gcnew RSAKeyValue( dynamic_cast<RSA^>(Key) ) ); // Add the KeyInfo object to the Reference object. XMLSignature->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Append the element to the XML document. doc->DocumentElement->AppendChild( doc->ImportNode( xmlDigitalSignature, true ) ); if ( dynamic_cast<XmlDeclaration^>(doc->FirstChild) ) { doc->RemoveChild( doc->FirstChild ); } // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmltw = gcnew XmlTextWriter( SignedFileName,gcnew UTF8Encoding( false ) ); doc->WriteTo( xmltw ); xmltw->Close(); } // Verify the signature of an XML file and return the result. static Boolean VerifyXmlFile( String^ Name ) { // Check the arguments. if ( Name == nullptr ) throw gcnew ArgumentNullException( L"Name" ); // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument; // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load( Name ); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml( xmlDocument ); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( L"Signature" ); // Load the signature node. signedXml->LoadXml( dynamic_cast<XmlElement^>(nodeList->Item( 0 )) ); // Check the signature and return the result. return signedXml->CheckSignature(); } int main() { // Generate a signing key. RSACryptoServiceProvider^ Key = gcnew RSACryptoServiceProvider; try { // Sign an XML file and save the signature to a // new file. SignXmlFile( L"Test.xml", L"SignedExample.xml", Key ); Console::WriteLine( L"XML file signed." ); // Verify the signature of the signed XML. Console::WriteLine( L"Verifying signature..." ); bool result = VerifyXmlFile( L"SignedExample.xml" ); // Display the results of the signature verification to // the console. if ( result ) { Console::WriteLine( L"The XML signature is valid." ); } else { Console::WriteLine( L"The XML signature is not valid." ); } } catch ( CryptographicException^ e ) { Console::WriteLine( e->Message ); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key->Clear(); } return 1; }
Signature クラスを SignedXml クラスと共に使用し、デタッチ シグネチャで XML ドキュメントの署名および検証を行うコードの例を次に示します。
' This example signs a URL using an ' envelope signature. It then verifies the ' signed XML. ' Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Text Imports System.Xml Module SignVerifyEnvelope Sub Main(ByVal args() As String) ' Generate a signing key. Dim Key As New RSACryptoServiceProvider() Try ' Sign the detached resource and save the signature in an XML file. SignDetachedResource("http://www.microsoft.com", "SignedExample.xml", Key) Console.WriteLine("XML file signed.") ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") Dim result As Boolean = VerifyXmlFile("SignedExample.xml") ' Display the results of the signature verification to \ ' the console. If result Then Console.WriteLine("The XML signature is valid.") Else Console.WriteLine("The XML signature is not valid.") End If Catch e As CryptographicException Console.WriteLine(e.Message) Finally ' Clear resources associated with the ' RSACryptoServiceProvider. Key.Clear() End Try End Sub ' Sign an XML file and save the signature in a new file. Sub SignDetachedResource(ByVal URIString As String, ByVal XmlSigFileName As String, ByVal Key As RSA) ' Check the arguments. If URIString Is Nothing Then Throw New ArgumentNullException("URIString") End If If XmlSigFileName Is Nothing Then Throw New ArgumentNullException("XmlSigFileName") End If If Key Is Nothing Then Throw New ArgumentNullException("Key") End If ' Create a SignedXml object. Dim signedXml As New SignedXml() ' Assign the key to the SignedXml object. signedXml.SigningKey = Key ' Get the signature object from the SignedXml object. Dim XMLSignature As Signature = signedXml.Signature ' Create a reference to be signed. Dim reference As New Reference() ' Add the passed URI to the reference object. reference.Uri = URIString ' Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference) ' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). Dim keyInfo As New KeyInfo() keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA))) ' Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo ' Compute the signature. signedXml.ComputeSignature() ' Get the XML representation of the signature and save ' it to an XmlElement object. Dim xmlDigitalSignature As XmlElement = signedXml.GetXml() ' Save the signed XML document to a file specified ' using the passed string. Dim xmltw As New XmlTextWriter(XmlSigFileName, New UTF8Encoding(False)) xmlDigitalSignature.WriteTo(xmltw) xmltw.Close() End Sub ' Verify the signature of an XML file and return the result. Function VerifyXmlFile(ByVal Name As String) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Format using white spaces. xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. xmlDocument.Load(Name) ' Create a new SignedXml object and pass it ' the XML document class. Dim signedXml As New SignedXml(xmlDocument) ' Find the "Signature" node and create a new ' XmlNodeList object. Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") ' Load the signature node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. Return signedXml.CheckSignature() End Function End Module
// // This example signs a URL using an // envelope signature. It then verifies the // signed XML. // using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Text; using System.Xml; public class SignVerifyEnvelope { public static void Main(String[] args) { // Generate a signing key. RSACryptoServiceProvider Key = new RSACryptoServiceProvider(); try { // Sign the detached resource and save the signature in an XML file. SignDetachedResource("http://www.microsoft.com", "SignedExample.xml", Key); Console.WriteLine("XML file signed."); // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification to \ // the console. if (result) { Console.WriteLine("The XML signature is valid."); } else { Console.WriteLine("The XML signature is not valid."); } } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key.Clear(); } } // Sign an XML file and save the signature in a new file. public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key) { // Check the arguments. if (URIString == null) throw new ArgumentNullException("URIString"); if (XmlSigFileName == null) throw new ArgumentNullException("XmlSigFileName"); if (Key == null) throw new ArgumentNullException("Key"); // Create a SignedXml object. SignedXml signedXml = new SignedXml(); // Assign the key to the SignedXml object. signedXml.SigningKey = Key; // Get the signature object from the SignedXml object. Signature XMLSignature = signedXml.Signature; // Create a reference to be signed. Reference reference = new Reference(); // Add the passed URI to the reference object. reference.Uri = URIString; // Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(new RSAKeyValue((RSA)Key)); // Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo; // Compute the signature. signedXml.ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml(); // Save the signed XML document to a file specified // using the passed string. XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false)); xmlDigitalSignature.WriteTo(xmltw); xmltw.Close(); } // Verify the signature of an XML file and return the result. public static Boolean VerifyXmlFile(String Name) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Format using white spaces. xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument.Load(Name); // Create a new SignedXml object and pass it // the XML document class. SignedXml signedXml = new SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature"); // Load the signature node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return signedXml.CheckSignature(); } }
// // This example signs a URL using an // envelope signature. It then verifies the // signed XML. // #using <System.dll> #using <System.Xml.dll> #using <System.Security.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; namespace Sample { public ref class SignVerifyEnvelope { public: static void Work() { // Generate a signing key. RSACryptoServiceProvider^ key = gcnew RSACryptoServiceProvider(); try { // Sign the detached resource and save the // signature in an XML file. SignDetachedResource("http://www.microsoft.com" , "SignedExample.xml", key); Console::WriteLine("XML file signed."); // Verify the signature of the signed XML. Console::WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification // to the console. if (result) { Console::WriteLine("The XML signature" " is valid."); } else { Console::WriteLine("The XML signature" " is not valid."); } Console::ReadLine(); } catch (CryptographicException^ ex) { Console::WriteLine(ex->Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. key->Clear(); } } // Sign an XML file and save the signature in a new file. static void SignDetachedResource(String^ uri, String^ xmlFileName, RSA^ key) { // Check the arguments. if (uri->Length == 0) { throw gcnew ArgumentException("uri"); } if (xmlFileName->Length == 0) { throw gcnew ArgumentException("xmlFileName"); } if (key->KeySize == 0) { throw gcnew ArgumentException("key"); } // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml(); // Assign the key to the SignedXml object. signedXml->SigningKey = key; // Get the signature object from the SignedXml object. Signature^ xmlSignature = signedXml->Signature; // Create a reference to be signed. Reference^ reference = gcnew Reference(); // Add the passed URI to the reference object. reference->Uri = uri; // Add the Reference object to the Signature object. xmlSignature->SignedInfo->AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient // find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo(); keyInfo->AddClause( gcnew RSAKeyValue(key)); // Add the KeyInfo object to the Reference object. xmlSignature->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmlTextWriter = gcnew XmlTextWriter( xmlFileName, gcnew UTF8Encoding(false)); xmlDigitalSignature->WriteTo(xmlTextWriter); xmlTextWriter->Close(); } // Verify the signature of an XML file and return the result. static Boolean VerifyXmlFile(String^ documentName) { // Check the arguments. if (documentName->Length == 0) { throw gcnew ArgumentException("documentName"); } // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument(); // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load(documentName); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName("Signature"); // Load the signature node. signedXml->LoadXml( (XmlElement^) nodeList->Item(0)); // Check the signature and return the result. return signedXml->CheckSignature(); } }; } int main() { Sample::SignVerifyEnvelope::Work(); }
継承階層
スレッド セーフ
プラットフォームWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照
構文
使用例Signature クラスを SignedXml クラスと共に使用し、エンベロープ シグネチャで XML ドキュメントの署名および検証を行うコードの例を次に示します。
' This example signs an XML file using an ' envelope signature. It then verifies the ' signed XML. ' Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Text Imports System.Xml Module SignVerifyEnvelope Sub Main(ByVal args() As String) ' Generate a signing key. Dim Key As New RSACryptoServiceProvider() Try ' Sign an XML file and save the signature to a ' new file. SignXmlFile("Test.xml", "SignedExample.xml", Key) Console.WriteLine("XML file signed.") ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") Dim result As Boolean = VerifyXmlFile("SignedExample.xml") ' Display the results of the signature verification to ' the console. If result Then Console.WriteLine("The XML signature is valid.") Else Console.WriteLine("The XML signature is not valid.") End If Catch e As CryptographicException Console.WriteLine(e.Message) Finally ' Clear resources associated with the ' RSACryptoServiceProvider. Key.Clear() End Try End Sub ' Sign an XML file and save the signature in a new file. Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal Key As RSA) ' Check the arguments. If FileName Is Nothing Then Throw New ArgumentNullException("FileName") End If If SignedFileName Is Nothing Then Throw New ArgumentNullException("SignedFileName") End If If Key Is Nothing Then Throw New ArgumentNullException("Key") End If ' Create a new XML document. Dim doc As New XmlDocument() ' Format the document to ignore white spaces. doc.PreserveWhitespace = False ' Load the passed XML file using it's name. doc.Load(New XmlTextReader(FileName)) ' Create a SignedXml object. Dim signedXml As New SignedXml(doc) ' Add the key to the SignedXml document. signedXml.SigningKey = Key ' Get the signature object from the SignedXml object. Dim XMLSignature As Signature = signedXml.Signature ' Create a reference to be signed. Pass "" ' to specify that all of the current XML ' document should be signed. Dim reference As New Reference("") ' Add an enveloped transformation to the reference. Dim env As New XmlDsigEnvelopedSignatureTransform() reference.AddTransform(env) ' Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference) ' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). Dim keyInfo As New KeyInfo() keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA))) ' Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo ' Compute the signature. signedXml.ComputeSignature() ' Get the XML representation of the signature and save ' it to an XmlElement object. Dim xmlDigitalSignature As XmlElement = signedXml.GetXml() ' Append the element to the XML document. doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True)) If TypeOf doc.FirstChild Is XmlDeclaration Then doc.RemoveChild(doc.FirstChild) End If ' Save the signed XML document to a file specified ' using the passed string. Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False)) doc.WriteTo(xmltw) xmltw.Close() End Sub ' Verify the signature of an XML file and return the result. Function VerifyXmlFile(ByVal Name As String) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Format using white spaces. xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. xmlDocument.Load(Name) ' Create a new SignedXml object and pass it ' the XML document class. Dim signedXml As New SignedXml(xmlDocument) ' Find the "Signature" node and create a new ' XmlNodeList object. Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") ' Load the signature node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. Return signedXml.CheckSignature() End Function End Module
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Text; using System.Xml; public class SignVerifyEnvelope { public static void Main(String[] args) { // Generate a signing key. RSACryptoServiceProvider Key = new RSACryptoServiceProvider(); try { // Sign an XML file and save the signature to a // new file. SignXmlFile("Test.xml", "SignedExample.xml", Key); Console.WriteLine("XML file signed."); // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification to // the console. if (result) { Console.WriteLine("The XML signature is valid."); } else { Console.WriteLine("The XML signature is not valid."); } } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key.Clear(); } } // Sign an XML file and save the signature in a new file. public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) { // Check the arguments. if (FileName == null) throw new ArgumentNullException("FileName"); if (SignedFileName == null) throw new ArgumentNullException("SignedFileName"); if (Key == null) throw new ArgumentNullException("Key"); // Create a new XML document. XmlDocument doc = new XmlDocument(); // Format the document to ignore white spaces. doc.PreserveWhitespace = false; // Load the passed XML file using it's name. doc.Load(new XmlTextReader(FileName)); // Create a SignedXml object. SignedXml signedXml = new SignedXml(doc); // Add the key to the SignedXml document. signedXml.SigningKey = Key; // Get the signature object from the SignedXml object. Signature XMLSignature = signedXml.Signature; // Create a reference to be signed. Pass "" // to specify that all of the current XML // document should be signed. Reference reference = new Reference(""); // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); // Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(new RSAKeyValue((RSA)Key)); // Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo; // Compute the signature. signedXml.ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml(); // Append the element to the XML document. doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); if (doc.FirstChild is XmlDeclaration) { doc.RemoveChild(doc.FirstChild); } // Save the signed XML document to a file specified // using the passed string. XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false)); doc.WriteTo(xmltw); xmltw.Close(); } // Verify the signature of an XML file and return the result. public static Boolean VerifyXmlFile(String Name) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Format using white spaces. xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument.Load(Name); // Create a new SignedXml object and pass it // the XML document class. SignedXml signedXml = new SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature"); // Load the signature node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return signedXml.CheckSignature(); } }
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // #using <System.Xml.dll> #using <System.Security.dll> #using <System.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; // Sign an XML file and save the signature in a new file. static void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key ) { // Check the arguments. if ( FileName == nullptr ) throw gcnew ArgumentNullException( L"FileName" ); if ( SignedFileName == nullptr ) throw gcnew ArgumentNullException( L"SignedFileName" ); if ( Key == nullptr ) throw gcnew ArgumentNullException( L"Key" ); // Create a new XML document. XmlDocument^ doc = gcnew XmlDocument; // Format the document to ignore white spaces. doc->PreserveWhitespace = false; // Load the passed XML file using it's name. doc->Load( gcnew XmlTextReader( FileName ) ); // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml( doc ); // Add the key to the SignedXml document. signedXml->SigningKey = Key; // Get the signature object from the SignedXml object. Signature^ XMLSignature = signedXml->Signature; // Create a reference to be signed. Pass "" // to specify that all of the current XML // document should be signed. Reference^ reference = gcnew Reference( L"" ); // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform^ env = gcnew XmlDsigEnvelopedSignatureTransform; reference->AddTransform( env ); // Add the Reference object to the Signature object. XMLSignature->SignedInfo->AddReference( reference ); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo; keyInfo->AddClause( gcnew RSAKeyValue( dynamic_cast<RSA^>(Key) ) ); // Add the KeyInfo object to the Reference object. XMLSignature->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Append the element to the XML document. doc->DocumentElement->AppendChild( doc->ImportNode( xmlDigitalSignature, true ) ); if ( dynamic_cast<XmlDeclaration^>(doc->FirstChild) ) { doc->RemoveChild( doc->FirstChild ); } // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmltw = gcnew XmlTextWriter( SignedFileName,gcnew UTF8Encoding( false ) ); doc->WriteTo( xmltw ); xmltw->Close(); } // Verify the signature of an XML file and return the result. static Boolean VerifyXmlFile( String^ Name ) { // Check the arguments. if ( Name == nullptr ) throw gcnew ArgumentNullException( L"Name" ); // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument; // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load( Name ); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml( xmlDocument ); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( L"Signature" ); // Load the signature node. signedXml->LoadXml( dynamic_cast<XmlElement^>(nodeList->Item( 0 )) ); // Check the signature and return the result. return signedXml->CheckSignature(); } int main() { // Generate a signing key. RSACryptoServiceProvider^ Key = gcnew RSACryptoServiceProvider; try { // Sign an XML file and save the signature to a // new file. SignXmlFile( L"Test.xml", L"SignedExample.xml", Key ); Console::WriteLine( L"XML file signed." ); // Verify the signature of the signed XML. Console::WriteLine( L"Verifying signature..." ); bool result = VerifyXmlFile( L"SignedExample.xml" ); // Display the results of the signature verification to // the console. if ( result ) { Console::WriteLine( L"The XML signature is valid." ); } else { Console::WriteLine( L"The XML signature is not valid." ); } } catch ( CryptographicException^ e ) { Console::WriteLine( e->Message ); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key->Clear(); } return 1; }
Signature クラスを SignedXml クラスと共に使用し、デタッチ シグネチャで XML ドキュメントの署名および検証を行うコードの例を次に示します。
' This example signs a URL using an ' envelope signature. It then verifies the ' signed XML. ' Imports System Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Imports System.Text Imports System.Xml Module SignVerifyEnvelope Sub Main(ByVal args() As String) ' Generate a signing key. Dim Key As New RSACryptoServiceProvider() Try ' Sign the detached resource and save the signature in an XML file. SignDetachedResource("http://www.microsoft.com", "SignedExample.xml", Key) Console.WriteLine("XML file signed.") ' Verify the signature of the signed XML. Console.WriteLine("Verifying signature...") Dim result As Boolean = VerifyXmlFile("SignedExample.xml") ' Display the results of the signature verification to \ ' the console. If result Then Console.WriteLine("The XML signature is valid.") Else Console.WriteLine("The XML signature is not valid.") End If Catch e As CryptographicException Console.WriteLine(e.Message) Finally ' Clear resources associated with the ' RSACryptoServiceProvider. Key.Clear() End Try End Sub ' Sign an XML file and save the signature in a new file. Sub SignDetachedResource(ByVal URIString As String, ByVal XmlSigFileName As String, ByVal Key As RSA) ' Check the arguments. If URIString Is Nothing Then Throw New ArgumentNullException("URIString") End If If XmlSigFileName Is Nothing Then Throw New ArgumentNullException("XmlSigFileName") End If If Key Is Nothing Then Throw New ArgumentNullException("Key") End If ' Create a SignedXml object. Dim signedXml As New SignedXml() ' Assign the key to the SignedXml object. signedXml.SigningKey = Key ' Get the signature object from the SignedXml object. Dim XMLSignature As Signature = signedXml.Signature ' Create a reference to be signed. Dim reference As New Reference() ' Add the passed URI to the reference object. reference.Uri = URIString ' Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference) ' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). Dim keyInfo As New KeyInfo() keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA))) ' Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo ' Compute the signature. signedXml.ComputeSignature() ' Get the XML representation of the signature and save ' it to an XmlElement object. Dim xmlDigitalSignature As XmlElement = signedXml.GetXml() ' Save the signed XML document to a file specified ' using the passed string. Dim xmltw As New XmlTextWriter(XmlSigFileName, New UTF8Encoding(False)) xmlDigitalSignature.WriteTo(xmltw) xmltw.Close() End Sub ' Verify the signature of an XML file and return the result. Function VerifyXmlFile(ByVal Name As String) As [Boolean] ' Check the arguments. If Name Is Nothing Then Throw New ArgumentNullException("Name") End If ' Create a new XML document. Dim xmlDocument As New XmlDocument() ' Format using white spaces. xmlDocument.PreserveWhitespace = True ' Load the passed XML file into the document. xmlDocument.Load(Name) ' Create a new SignedXml object and pass it ' the XML document class. Dim signedXml As New SignedXml(xmlDocument) ' Find the "Signature" node and create a new ' XmlNodeList object. Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature") ' Load the signature node. signedXml.LoadXml(CType(nodeList(0), XmlElement)) ' Check the signature and return the result. Return signedXml.CheckSignature() End Function End Module
// // This example signs a URL using an // envelope signature. It then verifies the // signed XML. // using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Text; using System.Xml; public class SignVerifyEnvelope { public static void Main(String[] args) { // Generate a signing key. RSACryptoServiceProvider Key = new RSACryptoServiceProvider(); try { // Sign the detached resource and save the signature in an XML file. SignDetachedResource("http://www.microsoft.com", "SignedExample.xml", Key); Console.WriteLine("XML file signed."); // Verify the signature of the signed XML. Console.WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification to \ // the console. if (result) { Console.WriteLine("The XML signature is valid."); } else { Console.WriteLine("The XML signature is not valid."); } } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. Key.Clear(); } } // Sign an XML file and save the signature in a new file. public static void SignDetachedResource(string URIString, string XmlSigFileName, RSA Key) { // Check the arguments. if (URIString == null) throw new ArgumentNullException("URIString"); if (XmlSigFileName == null) throw new ArgumentNullException("XmlSigFileName"); if (Key == null) throw new ArgumentNullException("Key"); // Create a SignedXml object. SignedXml signedXml = new SignedXml(); // Assign the key to the SignedXml object. signedXml.SigningKey = Key; // Get the signature object from the SignedXml object. Signature XMLSignature = signedXml.Signature; // Create a reference to be signed. Reference reference = new Reference(); // Add the passed URI to the reference object. reference.Uri = URIString; // Add the Reference object to the Signature object. XMLSignature.SignedInfo.AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(new RSAKeyValue((RSA)Key)); // Add the KeyInfo object to the Reference object. XMLSignature.KeyInfo = keyInfo; // Compute the signature. signedXml.ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml(); // Save the signed XML document to a file specified // using the passed string. XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false)); xmlDigitalSignature.WriteTo(xmltw); xmltw.Close(); } // Verify the signature of an XML file and return the result. public static Boolean VerifyXmlFile(String Name) { // Check the arguments. if (Name == null) throw new ArgumentNullException("Name"); // Create a new XML document. XmlDocument xmlDocument = new XmlDocument(); // Format using white spaces. xmlDocument.PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument.Load(Name); // Create a new SignedXml object and pass it // the XML document class. SignedXml signedXml = new SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature"); // Load the signature node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return signedXml.CheckSignature(); } }
// // This example signs a URL using an // envelope signature. It then verifies the // signed XML. // #using <System.dll> #using <System.Xml.dll> #using <System.Security.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; namespace Sample { public ref class SignVerifyEnvelope { public: static void Work() { // Generate a signing key. RSACryptoServiceProvider^ key = gcnew RSACryptoServiceProvider(); try { // Sign the detached resource and save the // signature in an XML file. SignDetachedResource("http://www.microsoft.com" , "SignedExample.xml", key); Console::WriteLine("XML file signed."); // Verify the signature of the signed XML. Console::WriteLine("Verifying signature..."); bool result = VerifyXmlFile("SignedExample.xml"); // Display the results of the signature verification // to the console. if (result) { Console::WriteLine("The XML signature" " is valid."); } else { Console::WriteLine("The XML signature" " is not valid."); } Console::ReadLine(); } catch (CryptographicException^ ex) { Console::WriteLine(ex->Message); } finally { // Clear resources associated with the // RSACryptoServiceProvider. key->Clear(); } } // Sign an XML file and save the signature in a new file. static void SignDetachedResource(String^ uri, String^ xmlFileName, RSA^ key) { // Check the arguments. if (uri->Length == 0) { throw gcnew ArgumentException("uri"); } if (xmlFileName->Length == 0) { throw gcnew ArgumentException("xmlFileName"); } if (key->KeySize == 0) { throw gcnew ArgumentException("key"); } // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml(); // Assign the key to the SignedXml object. signedXml->SigningKey = key; // Get the signature object from the SignedXml object. Signature^ xmlSignature = signedXml->Signature; // Create a reference to be signed. Reference^ reference = gcnew Reference(); // Add the passed URI to the reference object. reference->Uri = uri; // Add the Reference object to the Signature object. xmlSignature->SignedInfo->AddReference(reference); // Add an RSAKeyValue KeyInfo (optional; helps recipient // find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo(); keyInfo->AddClause( gcnew RSAKeyValue(key)); // Add the KeyInfo object to the Reference object. xmlSignature->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmlTextWriter = gcnew XmlTextWriter( xmlFileName, gcnew UTF8Encoding(false)); xmlDigitalSignature->WriteTo(xmlTextWriter); xmlTextWriter->Close(); } // Verify the signature of an XML file and return the result. static Boolean VerifyXmlFile(String^ documentName) { // Check the arguments. if (documentName->Length == 0) { throw gcnew ArgumentException("documentName"); } // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument(); // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load(documentName); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml(xmlDocument); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName("Signature"); // Load the signature node. signedXml->LoadXml( (XmlElement^) nodeList->Item(0)); // Check the signature and return the result. return signedXml->CheckSignature(); } }; } int main() { Sample::SignVerifyEnvelope::Work(); }
プラットフォームWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Id | 現在の Signature の ID を取得または設定します。 |
| KeyInfo | 現在の Signature の KeyInfo を取得または設定します。 |
| ObjectList | 署名されるオブジェクトのリストを取得または設定します。 |
| SignatureValue | デジタル署名の値を取得または設定します。 |
| SignedInfo | 現在の Signature の SignedInfo を取得または設定します。 |
参照
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| AddObject | 署名されるオブジェクトのリストに DataObject を追加します。 |
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
| GetXml | Signature の XML 表現を返します。 |
| LoadXml | XML 要素から Signature の状態を読み込みます。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
参照Signature データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Id | 現在の Signature の ID を取得または設定します。 |
| KeyInfo | 現在の Signature の KeyInfo を取得または設定します。 |
| ObjectList | 署名されるオブジェクトのリストを取得または設定します。 |
| SignatureValue | デジタル署名の値を取得または設定します。 |
| SignedInfo | 現在の Signature の SignedInfo を取得または設定します。 |
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| AddObject | 署名されるオブジェクトのリストに DataObject を追加します。 |
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
| GetXml | Signature の XML 表現を返します。 |
| LoadXml | XML 要素から Signature の状態を読み込みます。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
参照(signature から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2019/03/17 17:09 UTC 版)
ナビゲーションに移動 検索に移動数学、とくに線型代数学における符号数(ふごうすう、英: signature)は固有値の符号(正・負・零)を重複度を込めて数えたものである。
実有限次元線型空間上の、計量を与える実二次形式および付随する内積(実対称双線型形式)の符号数 (p, q, r) は、これを適当な基底に関して表示した時に得られる同伴実対称行列あるいはそれと同値な計量テンソルの、固有値の符号が正・負・零であるものがそれぞれ重複度込みで p, q, r 個であることを表す。これはそれぞれ正・負・零な部分空間のうち極大なものの次元と言ってもよい。シルヴェスターの慣性法則によれば、これらの数は基底のとり方に依らない。従って符号数は基底の取り方の違いに依らない計量を分類する。
複素係数の場合は、エルミート二次形式およびエルミート半双線型形式を考えれば、同様の結果を得る。
q = r = 0 のとき計量は正値あるいは正の定符号であるといい、p = r = 0 のとき負値あるいは負の定符号であるという。リーマン計量は定符号であるような計量テンソルである。ローレンツ計量は符号数 (p, 1) または (1, q) を持つものを言う。また、不定符号 (indefinite) あるいは混合型 (mixed) であるとは p, q が何れも非零であるときに言い、退化しているとは r が非零であるときに言う。定符号二次形式の項も参照。
「非退化」(r = 0)な計量に関して、符号数はしばしば(符号 0 に対応する部分を除く)整数の対として (p, q) と書いたり、あるいは符号数 (1, 3) や (3, 1) を固有値の符号列として明示的にそれぞれ (+, −, −, −) や (−, +, +, +) のように書いたりもする[1]。文献によっては p, q の代わりにひとつの数 s := p − q を符号数と呼ぶこともある。暗黙に全体の次元 n = p + q が与えられていると考えればこの s の意味での符号数から、上で述べた意味での符号数 (p, q) は復元できる。例えば, 符号数 s = 1 − 3 = −2 は (+, −, −, −) のことであり、s = 3 − 1 = +2 は (−, +, +, +) のことである。
スペクトル論によれば n × n 実対称行列は常に対角化可能であり、したがって(代数重複度を込めて)ちょうど n 個の実固有値を持つから、p + q + r = n を満たす。
シルヴェスターの慣性法則によれば、実対称双線型形式としての内積 g の符号数は基底の取り方に依らない。さらに言えば、計量 g が符号数 (p, q, r) を持つとき、
となるような基底が必ずとれる。これにより、等長同型 (V1, g1) → (V2, g2) が存在するための必要十分条件がg1 および g2 の符号数が等しいことであることが従う。同様にして、合同な行列の符号数は互いに等しく、合同を除いた行列の分類ができる。言葉を替えれば、二階共変対称テンソルの空間 S2V∗ への一般線型群 GL(V) の作用に関する軌道上で符号数は一定であり、これらの軌道を分類する。
符号数 (p,q,r) に対して、p は対称双線型形式 g がその上で正定値となるような部分線型空間の次元の最大値であり、同様に q は負定値となるような部分線型空間の最大値である。また r は g の根基(付随する対称行列の核空間)の次元である。従って、非退化な計量は符号数 (p, q, 0) を持ち、p + q = n を満たす。この特別の場合として (n, 0, 0) および (0, n, 0) はそれぞれ正定値および負定値の内積に対応し、負号反転によって互いに読み替えることができる。
次の二つの行列
はともに符号数 (1, 1, 0) を持つから、シルヴェスターの慣性法則によればこれらは互いに合同である。
数ベクトル空間 Rn の標準内積の符号数は (n, 0, 0) である。実対称双線型形式の意味での内積がこの符号数を持つための必要十分条件は、それが正定符号となることである。
負の定符号内積は符号数 (0, n, 0) を持つ。半負定符号内積は (p, 0, r) (p + r = n) を符号数に持つ。
ミンコフスキー空間は集合としては R4 であり、行列
の定める符号数 (3, 1, 0) の内積を持つ。符号を反転して符号数 (1, 3, 0) とすることもある。
行列の符号数の計算法はいくつかある。
数学においては正定値計量テンソルを備えたリーマン多様体を考えるのが普通である。
理論物理学では時空のモデルとして擬リーマン多様体を用いる。符号数は、時空が(特殊相対論に言う意味で)どのくらい空間的でどのくらい時間的であるかの指標として働く。素粒子物理学での用例では、計量は時間的部分空間上で正定値であり、空間的部分空間上で負定値である。特にミンコフスキー計量
を挙げれば、これは符号数 (1, 3, 0) で、時間方向には正定値、そのほかの三つの空間方向 x, y, z には負定値である。(ここでは s が固有時を直接的にはかるものとして与えているのでこうなるが、符号を逆にする流儀もある。)
計量が至る所正則ならば、計量は一定である。しかし、適当な超曲面上で計量が退化したり不連続になったりすることを許すならば、その計量の符号数はそれら曲面上で変化し得る[2]。そのような符号変化をもたらす行列は宇宙論や量子重力論に応用を持ち得る。