データのセキュリティ保護
Crypto クラスで提供されるメソッドを使用して、データを保護できます。
Crypto クラスのメソッドは、ダイジェスト、メッセージ認証コード、署名の作成、および情報の暗号化び復号化を行うための標準アルゴリズムを提供します。これらは、Force.com のコンテンツのセキュリティを確保したり、Google、Amazon WebServices (AWS) などの外部サービスと統合するために使用できます。
Amazon WebService のインテグレーションの例
次の例は、Salesforce と Amazon WebServices のインテグレーションを示しています。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class HMacAuthCallout {
18
19 public void testAlexaWSForAmazon() {
20
21 // The date format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
22 DateTime d = System.now();
23 String timestamp = ''+ d.year() + '-' +
24 d.month() + '-' +
25 d.day() + '\'T\'' +
26 d.hour() + ':' +
27 d.minute() + ':' +
28 d.second() + '.' +
29 d.millisecond() + '\'Z\'';
30 String timeFormat = d.formatGmt(timestamp);
31
32 String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');
33 String action = 'UrlInfo';
34 String inputStr = action + timeFormat;
35 String algorithmName = 'HMacSHA1';
36 Blob mac = Crypto.generateMac(algorithmName, Blob.valueOf(inputStr),
37 Blob.valueOf('your_signing_key'));
38 String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');
39
40 String urlToTest = 'amazon.com';
41 String version = '2005-07-11';
42 String endpoint = 'http://awis.amazonaws.com/';
43 String accessKey = 'your_key';
44
45 HttpRequest req = new HttpRequest();
46 req.setEndpoint(endpoint +
47 '?AWSAccessKeyId=' + accessKey +
48 '&Action=' + action +
49 '&ResponseGroup=Rank&Version=' + version +
50 '&Timestamp=' + urlEncodedTimestamp +
51 '&Url=' + urlToTest +
52 '&Signature=' + macUrl);
53
54 req.setMethod('GET');
55 Http http = new Http();
56 try {
57 HttpResponse res = http.send(req);
58 System.debug('STATUS:'+res.getStatus());
59 System.debug('STATUS_CODE:'+res.getStatusCode());
60 System.debug('BODY: '+res.getBody());
61 } catch(System.CalloutException e) {
62 System.debug('ERROR: '+ e);
63 }
64 }
65}
66暗号化および復号化の例
次の例では、Crypto クラスの encryptWithManagedIV メソッドと decryptWithManagedIV メソッド、および generateAesKey メソッドを使用します。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Use generateAesKey to generate the private key
18Blob cryptoKey = Crypto.generateAesKey(256);
19
20// Generate the data to be encrypted.
21Blob data = Blob.valueOf('Test data to encrypted');
22
23// Encrypt the data and have Salesforce.com generate the initialization vector
24Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
25
26// Decrypt the data
27Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);次は、encryptWithManagedIV および decryptWithManagedIV Crypto メソッドの単体テストの作成例です。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class CryptoTest {
19 static testMethod void testValidDecryption() {
20
21 // Use generateAesKey to generate the private key
22 Blob key = Crypto.generateAesKey(128);
23 // Generate the data to be encrypted.
24 Blob data = Blob.valueOf('Test data');
25 // Generate an encrypted form of the data using base64 encoding
26 String b64Data = EncodingUtil.base64Encode(data);
27 // Encrypt and decrypt the data
28 Blob encryptedData = Crypto.encryptWithManagedIV('AES128', key, data);
29 Blob decryptedData = Crypto.decryptWithManagedIV('AES128', key, encryptedData);
30 String b64Decrypted = EncodingUtil.base64Encode(decryptedData);
31 // Verify that the strings still match
32 System.assertEquals(b64Data, b64Decrypted);
33 }
34 static testMethod void testInvalidDecryption() {
35 // Verify that you must use the same key size for encrypting data
36 // Generate two private keys, using different key sizes
37 Blob keyOne = Crypto.generateAesKey(128);
38 Blob keyTwo = Crypto.generateAesKey(256);
39 // Generate the data to be encrypted.
40 Blob data = Blob.valueOf('Test data');
41 // Encrypt the data using the first key
42 Blob encryptedData = Crypto.encryptWithManagedIV('AES128', keyOne, data);
43 try {
44 // Try decrypting the data using the second key
45 Crypto.decryptWithManagedIV('AES256', keyTwo, encryptedData);
46 System.assert(false);
47 } catch(SecurityException e) {
48 System.assertEquals('Given final block not properly padded', e.getMessage());
49 }
50 }
51}