You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1017 lines
33 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>cipher - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package cipher</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "crypto/cipher"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package cipher implements standard block cipher modes that can be wrapped
  69. around low-level block cipher implementations.
  70. See <a href="http://csrc.nist.gov/groups/ST/toolkit/BCM/current_modes.html">http://csrc.nist.gov/groups/ST/toolkit/BCM/current_modes.html</a>
  71. and NIST Special Publication 800-38A.
  72. </p>
  73. </div>
  74. </div>
  75. <div id="pkg-index" class="toggleVisible">
  76. <div class="collapsed">
  77. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  78. </div>
  79. <div class="expanded">
  80. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  81. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  82. <div id="manual-nav">
  83. <dl>
  84. <dd><a href="index.html#AEAD">type AEAD</a></dd>
  85. <dd>&nbsp; &nbsp; <a href="index.html#NewGCM">func NewGCM(cipher Block) (AEAD, error)</a></dd>
  86. <dd>&nbsp; &nbsp; <a href="index.html#NewGCMWithNonceSize">func NewGCMWithNonceSize(cipher Block, size int) (AEAD, error)</a></dd>
  87. <dd><a href="index.html#Block">type Block</a></dd>
  88. <dd><a href="index.html#BlockMode">type BlockMode</a></dd>
  89. <dd>&nbsp; &nbsp; <a href="index.html#NewCBCDecrypter">func NewCBCDecrypter(b Block, iv []byte) BlockMode</a></dd>
  90. <dd>&nbsp; &nbsp; <a href="index.html#NewCBCEncrypter">func NewCBCEncrypter(b Block, iv []byte) BlockMode</a></dd>
  91. <dd><a href="index.html#Stream">type Stream</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#NewCFBDecrypter">func NewCFBDecrypter(block Block, iv []byte) Stream</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#NewCFBEncrypter">func NewCFBEncrypter(block Block, iv []byte) Stream</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#NewCTR">func NewCTR(block Block, iv []byte) Stream</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#NewOFB">func NewOFB(b Block, iv []byte) Stream</a></dd>
  96. <dd><a href="index.html#StreamReader">type StreamReader</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#StreamReader.Read">func (r StreamReader) Read(dst []byte) (n int, err error)</a></dd>
  98. <dd><a href="index.html#StreamWriter">type StreamWriter</a></dd>
  99. <dd>&nbsp; &nbsp; <a href="index.html#StreamWriter.Close">func (w StreamWriter) Close() error</a></dd>
  100. <dd>&nbsp; &nbsp; <a href="index.html#StreamWriter.Write">func (w StreamWriter) Write(src []byte) (n int, err error)</a></dd>
  101. </dl>
  102. </div><!-- #manual-nav -->
  103. <div id="pkg-examples">
  104. <h4>Examples</h4>
  105. <dl>
  106. <dd><a class="exampleLink" href="index.html#example_NewCBCDecrypter">NewCBCDecrypter</a></dd>
  107. <dd><a class="exampleLink" href="index.html#example_NewCBCEncrypter">NewCBCEncrypter</a></dd>
  108. <dd><a class="exampleLink" href="index.html#example_NewCFBDecrypter">NewCFBDecrypter</a></dd>
  109. <dd><a class="exampleLink" href="index.html#example_NewCFBEncrypter">NewCFBEncrypter</a></dd>
  110. <dd><a class="exampleLink" href="index.html#example_NewCTR">NewCTR</a></dd>
  111. <dd><a class="exampleLink" href="index.html#example_NewOFB">NewOFB</a></dd>
  112. <dd><a class="exampleLink" href="index.html#example_StreamReader">StreamReader</a></dd>
  113. <dd><a class="exampleLink" href="index.html#example_StreamWriter">StreamWriter</a></dd>
  114. </dl>
  115. </div>
  116. <h4>Package files</h4>
  117. <p>
  118. <span style="font-size:90%">
  119. <a href="http://localhost:6060/src/crypto/cipher/cbc.go">cbc.go</a>
  120. <a href="http://localhost:6060/src/crypto/cipher/cfb.go">cfb.go</a>
  121. <a href="http://localhost:6060/src/crypto/cipher/cipher.go">cipher.go</a>
  122. <a href="http://localhost:6060/src/crypto/cipher/ctr.go">ctr.go</a>
  123. <a href="http://localhost:6060/src/crypto/cipher/gcm.go">gcm.go</a>
  124. <a href="http://localhost:6060/src/crypto/cipher/io.go">io.go</a>
  125. <a href="http://localhost:6060/src/crypto/cipher/ofb.go">ofb.go</a>
  126. <a href="http://localhost:6060/src/crypto/cipher/xor.go">xor.go</a>
  127. </span>
  128. </p>
  129. </div><!-- .expanded -->
  130. </div><!-- #pkg-index -->
  131. <div id="pkg-callgraph" class="toggle" style="display: none">
  132. <div class="collapsed">
  133. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  134. </div> <!-- .expanded -->
  135. <div class="expanded">
  136. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  137. <p>
  138. In the call graph viewer below, each node
  139. is a function belonging to this package
  140. and its children are the functions it
  141. calls&mdash;perhaps dynamically.
  142. </p>
  143. <p>
  144. The root nodes are the entry points of the
  145. package: functions that may be called from
  146. outside the package.
  147. There may be non-exported or anonymous
  148. functions among them if they are called
  149. dynamically from another package.
  150. </p>
  151. <p>
  152. Click a node to visit that function's source code.
  153. From there you can visit its callers by
  154. clicking its declaring <code>func</code>
  155. token.
  156. </p>
  157. <p>
  158. Functions may be omitted if they were
  159. determined to be unreachable in the
  160. particular programs or tests that were
  161. analyzed.
  162. </p>
  163. <!-- Zero means show all package entry points. -->
  164. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  165. </div>
  166. </div> <!-- #pkg-callgraph -->
  167. <h2 id="AEAD">type <a href="http://localhost:6060/src/crypto/cipher/gcm.go?s=400:1713#L5">AEAD</a></h2>
  168. <pre>type AEAD interface {
  169. <span class="comment">// NonceSize returns the size of the nonce that must be passed to Seal</span>
  170. <span class="comment">// and Open.</span>
  171. NonceSize() <a href="../../builtin/index.html#int">int</a>
  172. <span class="comment">// Overhead returns the maximum difference between the lengths of a</span>
  173. <span class="comment">// plaintext and its ciphertext.</span>
  174. Overhead() <a href="../../builtin/index.html#int">int</a>
  175. <span class="comment">// Seal encrypts and authenticates plaintext, authenticates the</span>
  176. <span class="comment">// additional data and appends the result to dst, returning the updated</span>
  177. <span class="comment">// slice. The nonce must be NonceSize() bytes long and unique for all</span>
  178. <span class="comment">// time, for a given key.</span>
  179. <span class="comment">//</span>
  180. <span class="comment">// The plaintext and dst may alias exactly or not at all. To reuse</span>
  181. <span class="comment">// plaintext&#39;s storage for the encrypted output, use plaintext[:0] as dst.</span>
  182. Seal(dst, nonce, plaintext, additionalData []<a href="../../builtin/index.html#byte">byte</a>) []<a href="../../builtin/index.html#byte">byte</a>
  183. <span class="comment">// Open decrypts and authenticates ciphertext, authenticates the</span>
  184. <span class="comment">// additional data and, if successful, appends the resulting plaintext</span>
  185. <span class="comment">// to dst, returning the updated slice. The nonce must be NonceSize()</span>
  186. <span class="comment">// bytes long and both it and the additional data must match the</span>
  187. <span class="comment">// value passed to Seal.</span>
  188. <span class="comment">//</span>
  189. <span class="comment">// The ciphertext and dst may alias exactly or not at all. To reuse</span>
  190. <span class="comment">// ciphertext&#39;s storage for the decrypted output, use ciphertext[:0] as dst.</span>
  191. <span class="comment">//</span>
  192. <span class="comment">// Even if the function fails, the contents of dst, up to its capacity,</span>
  193. <span class="comment">// may be overwritten.</span>
  194. Open(dst, nonce, ciphertext, additionalData []<a href="../../builtin/index.html#byte">byte</a>) ([]<a href="../../builtin/index.html#byte">byte</a>, <a href="../../builtin/index.html#error">error</a>)
  195. }</pre>
  196. <p>
  197. AEAD is a cipher mode providing authenticated encryption with associated
  198. data. For a description of the methodology, see
  199. </p>
  200. <pre><a href="https://en.wikipedia.org/wiki/Authenticated_encryption">https://en.wikipedia.org/wiki/Authenticated_encryption</a>
  201. </pre>
  202. <h3 id="NewGCM">func <a href="http://localhost:6060/src/crypto/cipher/gcm.go?s=2973:3012#L67">NewGCM</a></h3>
  203. <pre>func NewGCM(cipher <a href="index.html#Block">Block</a>) (<a href="index.html#AEAD">AEAD</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  204. <p>
  205. NewGCM returns the given 128-bit, block cipher wrapped in Galois Counter Mode
  206. with the standard nonce length.
  207. </p>
  208. <h3 id="NewGCMWithNonceSize">func <a href="http://localhost:6060/src/crypto/cipher/gcm.go?s=3429:3491#L77">NewGCMWithNonceSize</a></h3>
  209. <pre>func NewGCMWithNonceSize(cipher <a href="index.html#Block">Block</a>, size <a href="../../builtin/index.html#int">int</a>) (<a href="index.html#AEAD">AEAD</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  210. <p>
  211. NewGCMWithNonceSize returns the given 128-bit, block cipher wrapped in Galois
  212. Counter Mode, which accepts nonces of the given length.
  213. </p>
  214. <p>
  215. Only use this function if you require compatibility with an existing
  216. cryptosystem that uses non-standard nonce lengths. All other users should use
  217. NewGCM, which is faster and more resistant to misuse.
  218. </p>
  219. <h2 id="Block">type <a href="http://localhost:6060/src/crypto/cipher/cipher.go?s=637:979#L5">Block</a></h2>
  220. <pre>type Block interface {
  221. <span class="comment">// BlockSize returns the cipher&#39;s block size.</span>
  222. BlockSize() <a href="../../builtin/index.html#int">int</a>
  223. <span class="comment">// Encrypt encrypts the first block in src into dst.</span>
  224. <span class="comment">// Dst and src may point at the same memory.</span>
  225. Encrypt(dst, src []<a href="../../builtin/index.html#byte">byte</a>)
  226. <span class="comment">// Decrypt decrypts the first block in src into dst.</span>
  227. <span class="comment">// Dst and src may point at the same memory.</span>
  228. Decrypt(dst, src []<a href="../../builtin/index.html#byte">byte</a>)
  229. }</pre>
  230. <p>
  231. A Block represents an implementation of block cipher
  232. using a given key. It provides the capability to encrypt
  233. or decrypt individual blocks. The mode implementations
  234. extend that capability to streams of blocks.
  235. </p>
  236. <h2 id="BlockMode">type <a href="http://localhost:6060/src/crypto/cipher/cipher.go?s=1518:1802#L30">BlockMode</a></h2>
  237. <pre>type BlockMode interface {
  238. <span class="comment">// BlockSize returns the mode&#39;s block size.</span>
  239. BlockSize() <a href="../../builtin/index.html#int">int</a>
  240. <span class="comment">// CryptBlocks encrypts or decrypts a number of blocks. The length of</span>
  241. <span class="comment">// src must be a multiple of the block size. Dst and src may point to</span>
  242. <span class="comment">// the same memory.</span>
  243. CryptBlocks(dst, src []<a href="../../builtin/index.html#byte">byte</a>)
  244. }</pre>
  245. <p>
  246. A BlockMode represents a block cipher running in a block-based mode (CBC,
  247. ECB etc).
  248. </p>
  249. <h3 id="NewCBCDecrypter">func <a href="http://localhost:6060/src/crypto/cipher/cbc.go?s=2098:2148#L71">NewCBCDecrypter</a></h3>
  250. <pre>func NewCBCDecrypter(b <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#BlockMode">BlockMode</a></pre>
  251. <p>
  252. NewCBCDecrypter returns a BlockMode which decrypts in cipher block chaining
  253. mode, using the given Block. The length of iv must be the same as the
  254. Block&#39;s block size and must match the iv used to encrypt the data.
  255. </p>
  256. <div id="example_NewCBCDecrypter" class="toggle">
  257. <div class="collapsed">
  258. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  259. </div>
  260. <div class="expanded">
  261. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  262. <p>Code:</p>
  263. <pre class="code">key := []byte(&#34;example key 1234&#34;)
  264. ciphertext, _ := hex.DecodeString(&#34;f363f3ccdcb12bb883abf484ba77d9cd7d32b5baecb3d4b1b3e0e4beffdb3ded&#34;)
  265. block, err := aes.NewCipher(key)
  266. if err != nil {
  267. panic(err)
  268. }
  269. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  270. <span class="comment">// include it at the beginning of the ciphertext.</span>
  271. if len(ciphertext) &lt; aes.BlockSize {
  272. panic(&#34;ciphertext too short&#34;)
  273. }
  274. iv := ciphertext[:aes.BlockSize]
  275. ciphertext = ciphertext[aes.BlockSize:]
  276. <span class="comment">// CBC mode always works in whole blocks.</span>
  277. if len(ciphertext)%aes.BlockSize != 0 {
  278. panic(&#34;ciphertext is not a multiple of the block size&#34;)
  279. }
  280. mode := cipher.NewCBCDecrypter(block, iv)
  281. <span class="comment">// CryptBlocks can work in-place if the two arguments are the same.</span>
  282. mode.CryptBlocks(ciphertext, ciphertext)
  283. <span class="comment">// If the original plaintext lengths are not a multiple of the block</span>
  284. <span class="comment">// size, padding would have to be added when encrypting, which would be</span>
  285. <span class="comment">// removed at this point. For an example, see</span>
  286. <span class="comment">// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. However, it&#39;s</span>
  287. <span class="comment">// critical to note that ciphertexts must be authenticated (i.e. by</span>
  288. <span class="comment">// using crypto/hmac) before being decrypted in order to avoid creating</span>
  289. <span class="comment">// a padding oracle.</span>
  290. fmt.Printf(&#34;%s\n&#34;, ciphertext)
  291. <span class="comment"></pre>
  292. <p>Output:</p>
  293. <pre class="output">exampleplaintext
  294. </pre>
  295. </div>
  296. </div>
  297. <h3 id="NewCBCEncrypter">func <a href="http://localhost:6060/src/crypto/cipher/cbc.go?s=849:899#L25">NewCBCEncrypter</a></h3>
  298. <pre>func NewCBCEncrypter(b <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#BlockMode">BlockMode</a></pre>
  299. <p>
  300. NewCBCEncrypter returns a BlockMode which encrypts in cipher block chaining
  301. mode, using the given Block. The length of iv must be the same as the
  302. Block&#39;s block size.
  303. </p>
  304. <div id="example_NewCBCEncrypter" class="toggle">
  305. <div class="collapsed">
  306. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  307. </div>
  308. <div class="expanded">
  309. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  310. <p>Code:</p>
  311. <pre class="code">
  312. key := []byte(&#34;example key 1234&#34;)
  313. plaintext := []byte(&#34;exampleplaintext&#34;)
  314. <span class="comment">// CBC mode works on blocks so plaintexts may need to be padded to the</span>
  315. <span class="comment">// next whole block. For an example of such padding, see</span>
  316. <span class="comment">// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. Here we&#39;ll</span>
  317. <span class="comment">// assume that the plaintext is already of the correct length.</span>
  318. if len(plaintext)%aes.BlockSize != 0 {
  319. panic(&#34;plaintext is not a multiple of the block size&#34;)
  320. }
  321. block, err := aes.NewCipher(key)
  322. if err != nil {
  323. panic(err)
  324. }
  325. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  326. <span class="comment">// include it at the beginning of the ciphertext.</span>
  327. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  328. iv := ciphertext[:aes.BlockSize]
  329. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  330. panic(err)
  331. }
  332. mode := cipher.NewCBCEncrypter(block, iv)
  333. mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
  334. <span class="comment">// It&#39;s important to remember that ciphertexts must be authenticated</span>
  335. <span class="comment">// (i.e. by using crypto/hmac) as well as being encrypted in order to</span>
  336. <span class="comment">// be secure.</span>
  337. fmt.Printf(&#34;%x\n&#34;, ciphertext)
  338. </pre>
  339. </div>
  340. </div>
  341. <h2 id="Stream">type <a href="http://localhost:6060/src/crypto/cipher/cipher.go?s=1021:1426#L19">Stream</a></h2>
  342. <pre>type Stream interface {
  343. <span class="comment">// XORKeyStream XORs each byte in the given slice with a byte from the</span>
  344. <span class="comment">// cipher&#39;s key stream. Dst and src may point to the same memory.</span>
  345. <span class="comment">// If len(dst) &lt; len(src), XORKeyStream should panic. It is acceptable</span>
  346. <span class="comment">// to pass a dst bigger than src, and in that case, XORKeyStream will</span>
  347. <span class="comment">// only update dst[:len(src)] and will not touch the rest of dst.</span>
  348. XORKeyStream(dst, src []<a href="../../builtin/index.html#byte">byte</a>)
  349. }</pre>
  350. <p>
  351. A Stream represents a stream cipher.
  352. </p>
  353. <h3 id="NewCFBDecrypter">func <a href="http://localhost:6060/src/crypto/cipher/cfb.go?s=1265:1316#L42">NewCFBDecrypter</a></h3>
  354. <pre>func NewCFBDecrypter(block <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#Stream">Stream</a></pre>
  355. <p>
  356. NewCFBDecrypter returns a Stream which decrypts with cipher feedback mode,
  357. using the given Block. The iv must be the same length as the Block&#39;s block
  358. size.
  359. </p>
  360. <div id="example_NewCFBDecrypter" class="toggle">
  361. <div class="collapsed">
  362. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  363. </div>
  364. <div class="expanded">
  365. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  366. <p>Code:</p>
  367. <pre class="code">key := []byte(&#34;example key 1234&#34;)
  368. ciphertext, _ := hex.DecodeString(&#34;22277966616d9bc47177bd02603d08c9a67d5380d0fe8cf3b44438dff7b9&#34;)
  369. block, err := aes.NewCipher(key)
  370. if err != nil {
  371. panic(err)
  372. }
  373. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  374. <span class="comment">// include it at the beginning of the ciphertext.</span>
  375. if len(ciphertext) &lt; aes.BlockSize {
  376. panic(&#34;ciphertext too short&#34;)
  377. }
  378. iv := ciphertext[:aes.BlockSize]
  379. ciphertext = ciphertext[aes.BlockSize:]
  380. stream := cipher.NewCFBDecrypter(block, iv)
  381. <span class="comment">// XORKeyStream can work in-place if the two arguments are the same.</span>
  382. stream.XORKeyStream(ciphertext, ciphertext)
  383. fmt.Printf(&#34;%s&#34;, ciphertext)
  384. <span class="comment"></pre>
  385. <p>Output:</p>
  386. <pre class="output">some plaintext
  387. </pre>
  388. </div>
  389. </div>
  390. <h3 id="NewCFBEncrypter">func <a href="http://localhost:6060/src/crypto/cipher/cfb.go?s=1010:1061#L35">NewCFBEncrypter</a></h3>
  391. <pre>func NewCFBEncrypter(block <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#Stream">Stream</a></pre>
  392. <p>
  393. NewCFBEncrypter returns a Stream which encrypts with cipher feedback mode,
  394. using the given Block. The iv must be the same length as the Block&#39;s block
  395. size.
  396. </p>
  397. <div id="example_NewCFBEncrypter" class="toggle">
  398. <div class="collapsed">
  399. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  400. </div>
  401. <div class="expanded">
  402. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  403. <p>Code:</p>
  404. <pre class="code">
  405. key := []byte(&#34;example key 1234&#34;)
  406. plaintext := []byte(&#34;some plaintext&#34;)
  407. block, err := aes.NewCipher(key)
  408. if err != nil {
  409. panic(err)
  410. }
  411. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  412. <span class="comment">// include it at the beginning of the ciphertext.</span>
  413. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  414. iv := ciphertext[:aes.BlockSize]
  415. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  416. panic(err)
  417. }
  418. stream := cipher.NewCFBEncrypter(block, iv)
  419. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  420. <span class="comment">// It&#39;s important to remember that ciphertexts must be authenticated</span>
  421. <span class="comment">// (i.e. by using crypto/hmac) as well as being encrypted in order to</span>
  422. <span class="comment">// be secure.</span>
  423. </pre>
  424. </div>
  425. </div>
  426. <h3 id="NewCTR">func <a href="http://localhost:6060/src/crypto/cipher/ctr.go?s=663:705#L16">NewCTR</a></h3>
  427. <pre>func NewCTR(block <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#Stream">Stream</a></pre>
  428. <p>
  429. NewCTR returns a Stream which encrypts/decrypts using the given Block in
  430. counter mode. The length of iv must be the same as the Block&#39;s block size.
  431. </p>
  432. <div id="example_NewCTR" class="toggle">
  433. <div class="collapsed">
  434. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  435. </div>
  436. <div class="expanded">
  437. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  438. <p>Code:</p>
  439. <pre class="code">key := []byte(&#34;example key 1234&#34;)
  440. plaintext := []byte(&#34;some plaintext&#34;)
  441. block, err := aes.NewCipher(key)
  442. if err != nil {
  443. panic(err)
  444. }
  445. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  446. <span class="comment">// include it at the beginning of the ciphertext.</span>
  447. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  448. iv := ciphertext[:aes.BlockSize]
  449. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  450. panic(err)
  451. }
  452. stream := cipher.NewCTR(block, iv)
  453. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  454. <span class="comment">// It&#39;s important to remember that ciphertexts must be authenticated</span>
  455. <span class="comment">// (i.e. by using crypto/hmac) as well as being encrypted in order to</span>
  456. <span class="comment">// be secure.</span>
  457. <span class="comment">// CTR mode is the same for both encryption and decryption, so we can</span>
  458. <span class="comment">// also decrypt that ciphertext with NewCTR.</span>
  459. plaintext2 := make([]byte, len(plaintext))
  460. stream = cipher.NewCTR(block, iv)
  461. stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
  462. fmt.Printf(&#34;%s\n&#34;, plaintext2)
  463. <span class="comment"></pre>
  464. <p>Output:</p>
  465. <pre class="output">some plaintext
  466. </pre>
  467. </div>
  468. </div>
  469. <h3 id="NewOFB">func <a href="http://localhost:6060/src/crypto/cipher/ofb.go?s=469:507#L9">NewOFB</a></h3>
  470. <pre>func NewOFB(b <a href="index.html#Block">Block</a>, iv []<a href="../../builtin/index.html#byte">byte</a>) <a href="index.html#Stream">Stream</a></pre>
  471. <p>
  472. NewOFB returns a Stream that encrypts or decrypts using the block cipher b
  473. in output feedback mode. The initialization vector iv&#39;s length must be equal
  474. to b&#39;s block size.
  475. </p>
  476. <div id="example_NewOFB" class="toggle">
  477. <div class="collapsed">
  478. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  479. </div>
  480. <div class="expanded">
  481. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  482. <p>Code:</p>
  483. <pre class="code">key := []byte(&#34;example key 1234&#34;)
  484. plaintext := []byte(&#34;some plaintext&#34;)
  485. block, err := aes.NewCipher(key)
  486. if err != nil {
  487. panic(err)
  488. }
  489. <span class="comment">// The IV needs to be unique, but not secure. Therefore it&#39;s common to</span>
  490. <span class="comment">// include it at the beginning of the ciphertext.</span>
  491. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  492. iv := ciphertext[:aes.BlockSize]
  493. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  494. panic(err)
  495. }
  496. stream := cipher.NewOFB(block, iv)
  497. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  498. <span class="comment">// It&#39;s important to remember that ciphertexts must be authenticated</span>
  499. <span class="comment">// (i.e. by using crypto/hmac) as well as being encrypted in order to</span>
  500. <span class="comment">// be secure.</span>
  501. <span class="comment">// OFB mode is the same for both encryption and decryption, so we can</span>
  502. <span class="comment">// also decrypt that ciphertext with NewOFB.</span>
  503. plaintext2 := make([]byte, len(plaintext))
  504. stream = cipher.NewOFB(block, iv)
  505. stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
  506. fmt.Printf(&#34;%s\n&#34;, plaintext2)
  507. <span class="comment"></pre>
  508. <p>Output:</p>
  509. <pre class="output">some plaintext
  510. </pre>
  511. </div>
  512. </div>
  513. <h2 id="StreamReader">type <a href="http://localhost:6060/src/crypto/cipher/io.go?s=426:477#L4">StreamReader</a></h2>
  514. <pre>type StreamReader struct {
  515. S <a href="index.html#Stream">Stream</a>
  516. R <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>
  517. }</pre>
  518. <p>
  519. StreamReader wraps a Stream into an io.Reader. It calls XORKeyStream
  520. to process each slice of data which passes through.
  521. </p>
  522. <div id="example_StreamReader" class="toggle">
  523. <div class="collapsed">
  524. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  525. </div>
  526. <div class="expanded">
  527. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  528. <p>Code:</p>
  529. <pre class="code">
  530. key := []byte(&#34;example key 1234&#34;)
  531. inFile, err := os.Open(&#34;encrypted-file&#34;)
  532. if err != nil {
  533. panic(err)
  534. }
  535. defer inFile.Close()
  536. block, err := aes.NewCipher(key)
  537. if err != nil {
  538. panic(err)
  539. }
  540. <span class="comment">// If the key is unique for each ciphertext, then it&#39;s ok to use a zero</span>
  541. <span class="comment">// IV.</span>
  542. var iv [aes.BlockSize]byte
  543. stream := cipher.NewOFB(block, iv[:])
  544. outFile, err := os.OpenFile(&#34;decrypted-file&#34;, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  545. if err != nil {
  546. panic(err)
  547. }
  548. defer outFile.Close()
  549. reader := &amp;cipher.StreamReader{S: stream, R: inFile}
  550. <span class="comment">// Copy the input file to the output file, decrypting as we go.</span>
  551. if _, err := io.Copy(outFile, reader); err != nil {
  552. panic(err)
  553. }
  554. <span class="comment">// Note that this example is simplistic in that it omits any</span>
  555. <span class="comment">// authentication of the encrypted data. If you were actually to use</span>
  556. <span class="comment">// StreamReader in this manner, an attacker could flip arbitrary bits in</span>
  557. <span class="comment">// the output.</span>
  558. </pre>
  559. </div>
  560. </div>
  561. <h3 id="StreamReader.Read">func (StreamReader) <a href="http://localhost:6060/src/crypto/cipher/io.go?s=479:536#L9">Read</a></h3>
  562. <pre>func (r <a href="index.html#StreamReader">StreamReader</a>) Read(dst []<a href="../../builtin/index.html#byte">byte</a>) (n <a href="../../builtin/index.html#int">int</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  563. <h2 id="StreamWriter">type <a href="http://localhost:6060/src/crypto/cipher/io.go?s=934:1010#L20">StreamWriter</a></h2>
  564. <pre>type StreamWriter struct {
  565. S <a href="index.html#Stream">Stream</a>
  566. W <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>
  567. Err <a href="../../builtin/index.html#error">error</a> <span class="comment">// unused</span>
  568. }</pre>
  569. <p>
  570. StreamWriter wraps a Stream into an io.Writer. It calls XORKeyStream
  571. to process each slice of data which passes through. If any Write call
  572. returns short then the StreamWriter is out of sync and must be discarded.
  573. A StreamWriter has no internal buffering; Close does not need
  574. to be called to flush write data.
  575. </p>
  576. <div id="example_StreamWriter" class="toggle">
  577. <div class="collapsed">
  578. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  579. </div>
  580. <div class="expanded">
  581. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  582. <p>Code:</p>
  583. <pre class="code">
  584. key := []byte(&#34;example key 1234&#34;)
  585. inFile, err := os.Open(&#34;plaintext-file&#34;)
  586. if err != nil {
  587. panic(err)
  588. }
  589. defer inFile.Close()
  590. block, err := aes.NewCipher(key)
  591. if err != nil {
  592. panic(err)
  593. }
  594. <span class="comment">// If the key is unique for each ciphertext, then it&#39;s ok to use a zero</span>
  595. <span class="comment">// IV.</span>
  596. var iv [aes.BlockSize]byte
  597. stream := cipher.NewOFB(block, iv[:])
  598. outFile, err := os.OpenFile(&#34;encrypted-file&#34;, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  599. if err != nil {
  600. panic(err)
  601. }
  602. defer outFile.Close()
  603. writer := &amp;cipher.StreamWriter{S: stream, W: outFile}
  604. <span class="comment">// Copy the input file to the output file, encrypting as we go.</span>
  605. if _, err := io.Copy(writer, inFile); err != nil {
  606. panic(err)
  607. }
  608. <span class="comment">// Note that this example is simplistic in that it omits any</span>
  609. <span class="comment">// authentication of the encrypted data. If you were actually to use</span>
  610. <span class="comment">// StreamReader in this manner, an attacker could flip arbitrary bits in</span>
  611. <span class="comment">// the decrypted result.</span>
  612. </pre>
  613. </div>
  614. </div>
  615. <h3 id="StreamWriter.Close">func (StreamWriter) <a href="http://localhost:6060/src/crypto/cipher/io.go?s=1395:1430#L40">Close</a></h3>
  616. <pre>func (w <a href="index.html#StreamWriter">StreamWriter</a>) Close() <a href="../../builtin/index.html#error">error</a></pre>
  617. <p>
  618. Close closes the underlying Writer and returns its Close return value, if the Writer
  619. is also an io.Closer. Otherwise it returns nil.
  620. </p>
  621. <h3 id="StreamWriter.Write">func (StreamWriter) <a href="http://localhost:6060/src/crypto/cipher/io.go?s=1012:1070#L26">Write</a></h3>
  622. <pre>func (w <a href="index.html#StreamWriter">StreamWriter</a>) Write(src []<a href="../../builtin/index.html#byte">byte</a>) (n <a href="../../builtin/index.html#int">int</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  623. <div id="footer">
  624. Build version go1.6.<br>
  625. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  626. the content of this page is licensed under the
  627. Creative Commons Attribution 3.0 License,
  628. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  629. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  630. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  631. </div>
  632. </div><!-- .container -->
  633. </div><!-- #page -->
  634. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  635. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  636. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  637. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  638. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  639. </body>
  640. </html>