History

Renamed folders and separated GSF.Media and GSF.Net into separate projects.

StephenCWills committed 14 years ago

Commit ac1ef661 parent 45bce55628 files · +733 −461Browse files

Showing the changes to Source/Libraries/GSF.Core/Security/Cryptography/SymmetricAlgorithmExtensions.cs (1 of 628 files). Show all

Unified · Split

Source/Libraries/GSF.Core/Security/Cryptography/SymmetricAlgorithmExtensions.cs+336−0
@@ -0,0 +1,336 @@
1+//*******************************************************************************************************
2+// SymmetricAlgorithmExtensions.cs - Gbtc
3+//
4+// Tennessee Valley Authority, 2009
5+// No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved.
6+//
7+// This software is made freely available under the TVA Open Source Agreement (see below).
8+//
9+// Code Modification History:
10+// -----------------------------------------------------------------------------------------------------
11+// 09/19/2008 - J. Ritchie Carroll
12+// Generated original version of source code.
13+// 09/14/2009 - Stephen C. Wills
14+// Added new header and license agreement.
15+//
16+//*******************************************************************************************************
17+
18+#region [ TVA Open Source Agreement ]
19+/*
20+
21+ THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,REPRODUCTION, DISTRIBUTION,
22+ MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE
23+ TENNESSEE VALLEY AUTHORITY, A CORPORATE AGENCY AND INSTRUMENTALITY OF THE UNITED STATES GOVERNMENT
24+ ("GOVERNMENT AGENCY"). GOVERNMENT AGENCY IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT
25+ DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO USES, REPRODUCES, DISTRIBUTES,
26+ MODIFIES OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT
27+ ACTION, ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
28+
29+ Original Software Designation: openPDC
30+ Original Software Title: The GSF Open Source Phasor Data Concentrator
31+ User Registration Requested. Please Visit https://naspi.tva.com/Registration/
32+ Point of Contact for Original Software: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>
33+
34+ 1. DEFINITIONS
35+
36+ A. "Contributor" means Government Agency, as the developer of the Original Software, and any entity
37+ that makes a Modification.
38+
39+ B. "Covered Patents" mean patent claims licensable by a Contributor that are necessarily infringed by
40+ the use or sale of its Modification alone or when combined with the Subject Software.
41+
42+ C. "Display" means the showing of a copy of the Subject Software, either directly or by means of an
43+ image, or any other device.
44+
45+ D. "Distribution" means conveyance or transfer of the Subject Software, regardless of means, to
46+ another.
47+
48+ E. "Larger Work" means computer software that combines Subject Software, or portions thereof, with
49+ software separate from the Subject Software that is not governed by the terms of this Agreement.
50+
51+ F. "Modification" means any alteration of, including addition to or deletion from, the substance or
52+ structure of either the Original Software or Subject Software, and includes derivative works, as that
53+ term is defined in the Copyright Statute, 17 USC § 101. However, the act of including Subject Software
54+ as part of a Larger Work does not in and of itself constitute a Modification.
55+
56+ G. "Original Software" means the computer software first released under this Agreement by Government
57+ Agency entitled openPDC, including source code, object code and accompanying documentation, if any.
58+
59+ H. "Recipient" means anyone who acquires the Subject Software under this Agreement, including all
60+ Contributors.
61+
62+ I. "Redistribution" means Distribution of the Subject Software after a Modification has been made.
63+
64+ J. "Reproduction" means the making of a counterpart, image or copy of the Subject Software.
65+
66+ K. "Sale" means the exchange of the Subject Software for money or equivalent value.
67+
68+ L. "Subject Software" means the Original Software, Modifications, or any respective parts thereof.
69+
70+ M. "Use" means the application or employment of the Subject Software for any purpose.
71+
72+ 2. GRANT OF RIGHTS
73+
74+ A. Under Non-Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor,
75+ with respect to its own contribution to the Subject Software, hereby grants to each Recipient a
76+ non-exclusive, world-wide, royalty-free license to engage in the following activities pertaining to
77+ the Subject Software:
78+
79+ 1. Use
80+
81+ 2. Distribution
82+
83+ 3. Reproduction
84+
85+ 4. Modification
86+
87+ 5. Redistribution
88+
89+ 6. Display
90+
91+ B. Under Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, with
92+ respect to its own contribution to the Subject Software, hereby grants to each Recipient under Covered
93+ Patents a non-exclusive, world-wide, royalty-free license to engage in the following activities
94+ pertaining to the Subject Software:
95+
96+ 1. Use
97+
98+ 2. Distribution
99+
100+ 3. Reproduction
101+
102+ 4. Sale
103+
104+ 5. Offer for Sale
105+
106+ C. The rights granted under Paragraph B. also apply to the combination of a Contributor's Modification
107+ and the Subject Software if, at the time the Modification is added by the Contributor, the addition of
108+ such Modification causes the combination to be covered by the Covered Patents. It does not apply to
109+ any other combinations that include a Modification.
110+
111+ D. The rights granted in Paragraphs A. and B. allow the Recipient to sublicense those same rights.
112+ Such sublicense must be under the same terms and conditions of this Agreement.
113+
114+ 3. OBLIGATIONS OF RECIPIENT
115+
116+ A. Distribution or Redistribution of the Subject Software must be made under this Agreement except for
117+ additions covered under paragraph 3H.
118+
119+ 1. Whenever a Recipient distributes or redistributes the Subject Software, a copy of this Agreement
120+ must be included with each copy of the Subject Software; and
121+
122+ 2. If Recipient distributes or redistributes the Subject Software in any form other than source code,
123+ Recipient must also make the source code freely available, and must provide with each copy of the
124+ Subject Software information on how to obtain the source code in a reasonable manner on or through a
125+ medium customarily used for software exchange.
126+
127+ B. Each Recipient must ensure that the following copyright notice appears prominently in the Subject
128+ Software:
129+
130+ No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved.
131+
132+ C. Each Contributor must characterize its alteration of the Subject Software as a Modification and
133+ must identify itself as the originator of its Modification in a manner that reasonably allows
134+ subsequent Recipients to identify the originator of the Modification. In fulfillment of these
135+ requirements, Contributor must include a file (e.g., a change log file) that describes the alterations
136+ made and the date of the alterations, identifies Contributor as originator of the alterations, and
137+ consents to characterization of the alterations as a Modification, for example, by including a
138+ statement that the Modification is derived, directly or indirectly, from Original Software provided by
139+ Government Agency. Once consent is granted, it may not thereafter be revoked.
140+
141+ D. A Contributor may add its own copyright notice to the Subject Software. Once a copyright notice has
142+ been added to the Subject Software, a Recipient may not remove it without the express permission of
143+ the Contributor who added the notice.
144+
145+ E. A Recipient may not make any representation in the Subject Software or in any promotional,
146+ advertising or other material that may be construed as an endorsement by Government Agency or by any
147+ prior Recipient of any product or service provided by Recipient, or that may seek to obtain commercial
148+ advantage by the fact of Government Agency's or a prior Recipient's participation in this Agreement.
149+
150+ F. In an effort to track usage and maintain accurate records of the Subject Software, each Recipient,
151+ upon receipt of the Subject Software, is requested to register with Government Agency by visiting the
152+ following website: https://naspi.tva.com/Registration/. Recipient's name and personal information
153+ shall be used for statistical purposes only. Once a Recipient makes a Modification available, it is
154+ requested that the Recipient inform Government Agency at the web site provided above how to access the
155+ Modification.
156+
157+ G. Each Contributor represents that that its Modification does not violate any existing agreements,
158+ regulations, statutes or rules, and further that Contributor has sufficient rights to grant the rights
159+ conveyed by this Agreement.
160+
161+ H. A Recipient may choose to offer, and to charge a fee for, warranty, support, indemnity and/or
162+ liability obligations to one or more other Recipients of the Subject Software. A Recipient may do so,
163+ however, only on its own behalf and not on behalf of Government Agency or any other Recipient. Such a
164+ Recipient must make it absolutely clear that any such warranty, support, indemnity and/or liability
165+ obligation is offered by that Recipient alone. Further, such Recipient agrees to indemnify Government
166+ Agency and every other Recipient for any liability incurred by them as a result of warranty, support,
167+ indemnity and/or liability offered by such Recipient.
168+
169+ I. A Recipient may create a Larger Work by combining Subject Software with separate software not
170+ governed by the terms of this agreement and distribute the Larger Work as a single product. In such
171+ case, the Recipient must make sure Subject Software, or portions thereof, included in the Larger Work
172+ is subject to this Agreement.
173+
174+ J. Notwithstanding any provisions contained herein, Recipient is hereby put on notice that export of
175+ any goods or technical data from the United States may require some form of export license from the
176+ U.S. Government. Failure to obtain necessary export licenses may result in criminal liability under
177+ U.S. laws. Government Agency neither represents that a license shall not be required nor that, if
178+ required, it shall be issued. Nothing granted herein provides any such export license.
179+
180+ 4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION
181+
182+ A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER
183+ EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT
184+ SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
185+ PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
186+ FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS
187+ AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR
188+ RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS
189+ RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND
190+ LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT
191+ "AS IS."
192+
193+ B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST GOVERNMENT AGENCY, ITS
194+ AGENTS, EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE
195+ OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM
196+ SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE
197+ SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS GOVERNMENT AGENCY, ITS AGENTS,
198+ EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY
199+ LAW. THE FOREGOING RELEASE AND INDEMNIFICATION SHALL APPLY EVEN IF THE LIABILITIES, DEMANDS, DAMAGES,
200+ EXPENSES OR LOSSES ARE CAUSED, OCCASIONED, OR CONTRIBUTED TO BY THE NEGLIGENCE, SOLE OR CONCURRENT, OF
201+ GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE
202+ IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.
203+
204+ 5. GENERAL TERMS
205+
206+ A. Termination: This Agreement and the rights granted hereunder will terminate automatically if a
207+ Recipient fails to comply with these terms and conditions, and fails to cure such noncompliance within
208+ thirty (30) days of becoming aware of such noncompliance. Upon termination, a Recipient agrees to
209+ immediately cease use and distribution of the Subject Software. All sublicenses to the Subject
210+ Software properly granted by the breaching Recipient shall survive any such termination of this
211+ Agreement.
212+
213+ B. Severability: If any provision of this Agreement is invalid or unenforceable under applicable law,
214+ it shall not affect the validity or enforceability of the remainder of the terms of this Agreement.
215+
216+ C. Applicable Law: This Agreement shall be subject to United States federal law only for all purposes,
217+ including, but not limited to, determining the validity of this Agreement, the meaning of its
218+ provisions and the rights, obligations and remedies of the parties.
219+
220+ D. Entire Understanding: This Agreement constitutes the entire understanding and agreement of the
221+ parties relating to release of the Subject Software and may not be superseded, modified or amended
222+ except by further written agreement duly executed by the parties.
223+
224+ E. Binding Authority: By accepting and using the Subject Software under this Agreement, a Recipient
225+ affirms its authority to bind the Recipient to all terms and conditions of this Agreement and that
226+ Recipient hereby agrees to all terms and conditions herein.
227+
228+ F. Point of Contact: Any Recipient contact with Government Agency is to be directed to the designated
229+ representative as follows: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>.
230+
231+*/
232+#endregion
233+
234+using System.IO;
235+using System.Security.Cryptography;
236+
237+namespace GSF.Security.Cryptography
238+{
239+ /// <summary>
240+ /// Defines extension functions related to cryptographic <see cref="SymmetricAlgorithm"/> objects.
241+ /// </summary>
242+ public static class SymmetricAlgorithmExtensions
243+ {
244+ /// <summary>
245+ /// Returns a binary array of encrypted data for the given parameters.
246+ /// </summary>
247+ /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for encryption.</param>
248+ /// <param name="data">Source buffer containing data to encrypt.</param>
249+ /// <param name="startIndex">Offset into <paramref name="data"/> buffer.</param>
250+ /// <param name="length">Number of bytes in <paramref name="data"/> buffer to encrypt starting from <paramref name="startIndex"/> offset.</param>
251+ /// <param name="key">The secret key to use for the symmetric algorithm.</param>
252+ /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
253+ /// <returns>Encrypted version of <paramref name="data"/> buffer.</returns>
254+ public static byte[] Encrypt(this SymmetricAlgorithm algorithm, byte[] data, int startIndex, int length, byte[] key, byte[] iv)
255+ {
256+ MemoryStream source = new MemoryStream(data, startIndex, length);
257+ MemoryStream destination = new MemoryStream();
258+
259+ algorithm.Encrypt(source, destination, key, iv);
260+
261+ return destination.ToArray();
262+ }
263+
264+ /// <summary>
265+ /// Encrypts input stream onto output stream for the given parameters.
266+ /// </summary>
267+ /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for encryption.</param>
268+ /// <param name="source">Source stream that contains data to encrypt.</param>
269+ /// <param name="destination">Destination stream used to hold encrypted data.</param>
270+ /// <param name="key">The secret key to use for the symmetric algorithm.</param>
271+ /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
272+ public static void Encrypt(this SymmetricAlgorithm algorithm, Stream source, Stream destination, byte[] key, byte[] iv)
273+ {
274+ byte[] buffer = new byte[Standard.BufferSize];
275+ CryptoStream encodeStream = new CryptoStream(destination, algorithm.CreateEncryptor(key, iv), CryptoStreamMode.Write);
276+ int read;
277+
278+ // Encrypts data onto output stream.
279+ read = source.Read(buffer, 0, Standard.BufferSize);
280+
281+ while (read > 0)
282+ {
283+ encodeStream.Write(buffer, 0, read);
284+ read = source.Read(buffer, 0, Standard.BufferSize);
285+ }
286+
287+ encodeStream.FlushFinalBlock();
288+ }
289+
290+ /// <summary>
291+ /// Returns a binary array of decrypted data for the given parameters.
292+ /// </summary>
293+ /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for decryption.</param>
294+ /// <param name="data">Source buffer containing data to decrypt.</param>
295+ /// <param name="startIndex">Offset into <paramref name="data"/> buffer.</param>
296+ /// <param name="length">Number of bytes in <paramref name="data"/> buffer to decrypt starting from <paramref name="startIndex"/> offset.</param>
297+ /// <param name="key">The secret key to use for the symmetric algorithm.</param>
298+ /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
299+ /// <returns>Decrypted version of <paramref name="data"/> buffer.</returns>
300+ public static byte[] Decrypt(this SymmetricAlgorithm algorithm, byte[] data, int startIndex, int length, byte[] key, byte[] iv)
301+ {
302+ MemoryStream source = new MemoryStream(data, startIndex, length);
303+ MemoryStream destination = new MemoryStream();
304+
305+ algorithm.Decrypt(source, destination, key, iv);
306+
307+ return destination.ToArray();
308+ }
309+
310+ /// <summary>
311+ /// Decrypts input stream onto output stream for the given parameters.
312+ /// </summary>
313+ /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for decryption.</param>
314+ /// <param name="source">Source stream that contains data to decrypt.</param>
315+ /// <param name="destination">Destination stream used to hold decrypted data.</param>
316+ /// <param name="key">The secret key to use for the symmetric algorithm.</param>
317+ /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
318+ public static void Decrypt(this SymmetricAlgorithm algorithm, Stream source, Stream destination, byte[] key, byte[] iv)
319+ {
320+ byte[] buffer = new byte[Standard.BufferSize];
321+ CryptoStream decodeStream = new CryptoStream(destination, algorithm.CreateDecryptor(key, iv), CryptoStreamMode.Write);
322+ int read;
323+
324+ // Decrypts data onto output stream.
325+ read = source.Read(buffer, 0, Standard.BufferSize);
326+
327+ while (read > 0)
328+ {
329+ decodeStream.Write(buffer, 0, read);
330+ read = source.Read(buffer, 0, Standard.BufferSize);
331+ }
332+
333+ decodeStream.FlushFinalBlock();
334+ }
335+ }
336+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t