forked from apache/shiro-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-cryptography-guide.md.vtl
105 lines (71 loc) · 4.1 KB
/
java-cryptography-guide.md.vtl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<a name="JavaCryptographyGuide-JavaCryptographyGuidewithApacheShiro"></a>
Java Cryptography Guide with Apache Shiro
=========================================
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_compact" href="http://www.addthis.com/bookmark.php?v=250&pubid=ra-4d66ef016022c3bd">Share</a>
<span class="addthis_separator">|</span>
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
</div>
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4d66ef016022c3bd"></script>
Cryptography is the protecting of information from undesired access by hiding it or converting it into nonsense so that no one can read it.
Shiro is a major part of Shiro because we wanted to provide you with simplicity on what is typically a very complex topic. For example, the Java Cryptophay Environments (JCE) already handles cryptogrpahy in a Java environment but is very difficult to learn and use. So we grabbed the concepts made available by the JCE API and make them available to us mortals. In addition, all of the calls in the JCE are procedural which doesn't fit in Java's Object Oriented paradigm. So in Shiro, our cryptography features are all object oriented.
<a name="JavaCryptographyGuide-ElementsofCryptography"></a>
Elements of Cryptography
------------------------
Cryptogrpahy has two core elements in Shiro-- ciphers and hashes.
<a name="JavaCryptographyGuide-CiphersDefined"></a>
#[[###Ciphers Defined]]#
Ciphers are algorightms that can either encrypt or decrypt based on public or private key pair. And there are two different types of ciphers:
* Symmetric Cipher - encrypts and decrypts using the same key.
* Asymmetric Cipher - uses different keys for encryption and decryption.
Both cipher type are support in Shiro.
<a name="JavaCryptographyGuide-HashesDefined"></a>
#[[###Hashes Defined]]#
A hash is a one-way irreversible conversion of an input source. In the JDK, a hash is referred to as a message digest. A cryptographic hash and a message digests are the same thing and both terms or correct.
<a name="JavaCryptographyGuide-CommonusesforHashes"></a>
#[[####Common uses for Hashes]]#
Hashes are often used to transforms credentials like passwords or biometric data. It's a one way transformation so you can never see what the original value was. This is a very safe way of storing passwords so that no one other than the user will ever know a password, even if your system is compromised.
In addition, Shiro's hashes can be used with any type of data with an underlying byte array. Examples of this data include files, streams, byte arrays, strings, and character arrays.
<a name="JavaCryptographyGuide-CipherFeatures"></a>
Cipher Features
---------------
<a name="JavaCryptographyGuide-Shiro%27sCipherServiceInterface"></a>
#[[###Shiro's CipherService Interface]]#
``` java
public interface CipherService {
ByteSource encrypt( byte[] raw, byte[] key);
void encrypt(InputStream in, OutputStream out, byte[] key);
ByteSource decrypt( byte[] cipherText, byte[] key);
void decrypt(InputStream in, OutputStream out, byte[] key);
}
```
<a name="JavaCryptographyGuide-HashFeatures"></a>
Hash Features
-------------
#tip('Tip', 'Salts are important when hashing ...')
#tip('Tip', 'Repeated hashes are important when hashing ...')
<a name="JavaCryptographyGuide-Shiro%27sHashInterface"></a>
#[[###Shiro's Hash Interface]]#
``` java
public interface Hash {
byte[] getBytes();
String toHex();
String toBase64();
}
```
<a name="JavaCryptographyGuide-ExamplesofhowtouseHashesinyourcode"></a>
#[[###Examples of how to use Hashes in your code]]#
``` java
//some examples:
new Md5Hash(“foo”).toHex();
//File MD5 Hash value for checksum:
new MD5Hash( aFile ).toHex();
//store a password, but not raw:
new Sha256(aPassword, salt, 1024).toBase64();
```
#lendAHandDoc()
<input type="hidden" id="ghEditPage" value="java-cryptography-guide.md.vtl"></input>