237 lines
No EOL
12 KiB
HTML
237 lines
No EOL
12 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||
|
||
|
||
<meta name="keywords" content="iPhone, Objective-C, programmation">
|
||
|
||
<link rel="shortcut icon" type="image/x-icon" href="/Scratch/img/favicon.ico" />
|
||
<link rel="stylesheet" type="text/css" href="/Scratch/assets/css/main.css" />
|
||
<link rel="stylesheet" type="text/css" href="/Scratch/css/twilight.css" />
|
||
<link rel="stylesheet" type="text/css" href="/Scratch/css/idc.css" />
|
||
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://feeds.feedburner.com/yannespositocomfr"/>
|
||
|
||
<link rel="alternate" lang="fr" xml:lang="fr" title="base64 et sha1 sur iPhone" type="text/html" hreflang="fr" href="/Scratch/fr/blog/2010-09-02-base64-and-sha1-on-iPhone/" />
|
||
<link rel="alternate" lang="en" xml:lang="en" title="base64 and sha1 on iPhone" type="text/html" hreflang="en" href="/Scratch/en/blog/2010-09-02-base64-and-sha1-on-iPhone/" />
|
||
<script type="text/javascript" src="/Scratch/js/jquery-1.3.1.min.js"></script>
|
||
<script type="text/javascript" src="/Scratch/js/jquery.cookie.js"></script>
|
||
<script type="text/javascript" src="/Scratch/js/index.js"></script>
|
||
<!--[if lt IE 9]>
|
||
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
|
||
<![endif]-->
|
||
<title>base64 et sha1 sur iPhone</title>
|
||
</head>
|
||
<body lang="fr" class="article">
|
||
<script type="text/javascript">// <![CDATA[
|
||
document.write('<div id="blackpage"><img src="/Scratch/img/loading.gif" alt="Chargement en cours..."/></div>');
|
||
// ]]>
|
||
</script>
|
||
|
||
<div id="content">
|
||
|
||
<div id="choix">
|
||
<div class="return"><a href="#entete">↓ Menu ↓</a></div>
|
||
<div id="choixlang">
|
||
<a href="/Scratch/en/blog/2010-09-02-base64-and-sha1-on-iPhone/" onclick="setLanguage('en')">in English</a>
|
||
</div>
|
||
<div class="flush"></div>
|
||
</div>
|
||
<div id="titre">
|
||
<h1>
|
||
base64 et sha1 sur iPhone
|
||
</h1>
|
||
|
||
</div>
|
||
<div class="flush"></div>
|
||
|
||
|
||
|
||
|
||
|
||
<div class="flush"></div>
|
||
<div id="afterheader">
|
||
<div class="corps">
|
||
<p>Allons directement à l’essentiel :
|
||
voici deux fonctions à intégrer à votre application iPhone pour afficher l’encodage en base64 ou en hexadecimal du hash sha1 d’un string en Objective-C pour iPhone.</p>
|
||
|
||
<p>Pour l’usage c’est très simple, copiez le code dans la classe de votre choix.
|
||
Puis :</p>
|
||
|
||
<pre class="twilight">
|
||
#import <CommonCrypto/CommonDigest.h>
|
||
...
|
||
<span class="Support">NSString</span> *b64_hash = [<span class="Variable">self</span> <span class="SupportFunction">b64_sha1<span class="SupportFunction">:</span></span><span class="String"><span class="String">@"</span>some NSString to be sha1'ed<span class="String">"</span></span>];
|
||
...
|
||
<span class="Support">NSString</span> *hex_hash = [<span class="Variable">self</span> <span class="SupportFunction">hex_sha1<span class="SupportFunction">:</span></span><span class="String"><span class="String">@"</span>some NSString to be sha1'ed<span class="String">"</span></span>];
|
||
</pre>
|
||
|
||
<p>L’algorithme pour l’encodage en <code>base64</code> doit être programmé sur iPhone.
|
||
Il n’y a pas de librairie officielle qui s’occupe de ça.</p>
|
||
|
||
<div class="code"><div class="file"><a href="/Scratch/fr/blog/2010-09-02-base64-and-sha1-on-iPhone/code/iphone_base64_sha1.c"> ➥ iphone_base64_sha1.c </a></div><div class="withfile">
|
||
<pre class="twilight">
|
||
|
||
- (unsigned char *)sha1:(<span class="Support">NSString</span> *)baseString result:(unsigned char *)result {
|
||
char *c_baseString=(char *)[baseString <span class="SupportFunction">UTF8String</span>];
|
||
CC_SHA1(c_baseString, strlen(c_baseString), result);
|
||
return result;
|
||
}
|
||
|
||
- (<span class="Support">NSString</span> *)base64:(unsigned char *)result {
|
||
<span class="Support">NSString</span> *password=[[<span class="Support">NSString</span> <span class="SupportFunction">alloc</span>] <span class="SupportFunction">init</span>];
|
||
static const unsigned char cb64[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||
for (int i=0; i<CC_SHA1_DIGEST_LENGTH; i+=3) {
|
||
password=[password <span class="SupportFunction">stringByAppendingFormat<span class="SupportFunction">:</span></span><span class="String"><span class="String">@"</span>%c%c%c%c<span class="String">"</span></span>,
|
||
cb64[(result[i] &0xFC)>>2],
|
||
cb64[((result[i] & 0x03) << 4)
|
||
| ((result[i + 1] & 0xF0) >> 4)],
|
||
cb64[((result[i + 1] & 0x0F) << 2)
|
||
| ((result[i + 2] & 0xC0) >> 6)],
|
||
cb64[result[i+2]&0x3F]
|
||
];
|
||
}
|
||
return password;
|
||
}
|
||
|
||
- (<span class="Support">NSString</span> *)hexadecimalRepresentation:(unsigned char *)result {
|
||
<span class="Support">NSString</span> *password=[[<span class="Support">NSString</span> <span class="SupportFunction">alloc</span>] <span class="SupportFunction">init</span>];
|
||
for (int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
|
||
password=[password <span class="SupportFunction">stringByAppendingFormat<span class="SupportFunction">:</span></span><span class="String"><span class="String">@"</span>%02x<span class="String">"</span></span>, result[i]];
|
||
}
|
||
return password;
|
||
}
|
||
|
||
- (<span class="Support">NSString</span> *)b64_sha1:(<span class="Support">NSString</span> *)inputString {
|
||
unsigned char result[CC_SHA1_DIGEST_LENGTH+1];
|
||
[<span class="Variable">self</span> <span class="SupportFunction">sha1<span class="SupportFunction">:</span></span>inputString <span class="SupportFunction">result<span class="SupportFunction">:</span></span>result];
|
||
return [<span class="Variable">self</span> <span class="SupportFunction">base64<span class="SupportFunction">:</span></span>result];
|
||
}
|
||
|
||
- (<span class="Support">NSString</span> *)hex_sha1:(<span class="Support">NSString</span> *)inputString {
|
||
unsigned char result[CC_SHA1_DIGEST_LENGTH+1];
|
||
[<span class="Variable">self</span> <span class="SupportFunction">sha1<span class="SupportFunction">:</span></span>inputString <span class="SupportFunction">result<span class="SupportFunction">:</span></span>result];
|
||
return [<span class="Variable">self</span> <span class="SupportFunction">hexadecimalRepresentation<span class="SupportFunction">:</span></span>result];
|
||
}
|
||
</pre>
|
||
</div></div>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<div id="choixrss">
|
||
<a id="rss" href="http://feeds.feedburner.com/yannespositocomfr">
|
||
s'abonner
|
||
</a>
|
||
</div>
|
||
<script type="text/javascript">
|
||
$(document).ready(function(){
|
||
$('#comment').hide();
|
||
$('#clickcomment').click(showComments);
|
||
});
|
||
function showComments() {
|
||
$('#comment').show();
|
||
$('#clickcomment').fadeOut();
|
||
}
|
||
document.write('<div id="clickcomment">Commentaires</div>');
|
||
</script>
|
||
<div class="flush"></div>
|
||
<div class="corps" id="comment">
|
||
<h2 class="first">commentaires</h2>
|
||
<noscript>
|
||
Vous devez activer javascript pour commenter.
|
||
</noscript>
|
||
|
||
<script type="text/javascript">
|
||
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
|
||
var idcomments_post_id = '/Scratch/fr/blog/2010-09-02-base64-and-sha1-on-iPhone/';
|
||
var idcomments_post_url = 'http://yannesposito.com/Scratch/fr/blog/2010-09-02-base64-and-sha1-on-iPhone/';
|
||
</script>
|
||
<span id="IDCommentsPostTitle" style="display:none"></span>
|
||
<script type='text/javascript' src='/Scratch/js/genericCommentWrapperV2.js'></script>
|
||
|
||
</div>
|
||
|
||
<div id="entete" class="corps_spaced">
|
||
<div id="liens">
|
||
<ul><li><a href="/Scratch/fr/">Bienvenue</a></li>
|
||
<li><a href="/Scratch/fr/blog/">Blog</a></li>
|
||
<li><a href="/Scratch/fr/softwares/">Softwares</a></li>
|
||
<li><a href="/Scratch/fr/about/">À propos</a></li></ul>
|
||
</div>
|
||
<div class="flush"></div>
|
||
<hr/>
|
||
<div id="next_before_articles">
|
||
<div id="previous_articles">
|
||
articles précédents
|
||
|
||
<div class="previous_article">
|
||
<a href="/Scratch/fr/blog/2010-08-31-send-mail-from-command-line-with-attached-file/"><span class="nicer">«</span> Envoyer un mail en ligne de commande avec un fichier attaché</a>
|
||
</div>
|
||
|
||
|
||
<div class="previous_article">
|
||
<a href="/Scratch/fr/blog/2010-08-23-Now-heberged-on-heroku/"><span class="nicer">«</span> Maintenant sur Heroku</a>
|
||
</div>
|
||
|
||
|
||
<div class="previous_article">
|
||
<a href="/Scratch/fr/blog/2010-07-09-Indecidabilities/"><span class="nicer">«</span> Indécidabilités (partie 1)</a>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
<div id="next_articles">
|
||
articles suivants
|
||
|
||
<div class="next_article">
|
||
<a href="/Scratch/fr/blog/2010-09-02-Use-git-to-calculate-trusted-mtimes/">Utilisation de git pour calculer les mtimes <span class="nicer">»</span></a>
|
||
</div>
|
||
|
||
|
||
<div class="next_article">
|
||
<a href="/Scratch/fr/blog/2010-10-06-New-Blog-Design-Constraints/">Contraintes du design de ce blog <span class="nicer">»</span></a>
|
||
</div>
|
||
|
||
|
||
<div class="next_article">
|
||
<a href="/Scratch/fr/blog/2010-10-10-Secure-eMail-on-Mac-in-few-steps/">Sécurisez vos emails <span class="nicer">»</span></a>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
<div class="flush"></div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div id="bottom">
|
||
<div>
|
||
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.fr">Droits de reproduction ©, Yann Esposito</a>
|
||
</div>
|
||
<div id="lastmod">
|
||
Écrit le : 02/09/2010
|
||
modifié le : 16/11/2011
|
||
</div>
|
||
<div>
|
||
Site entièrement réalisé avec
|
||
<a href="http://www.vim.org">Vim</a>
|
||
et
|
||
<a href="http://nanoc.stoneship.org">nanoc</a>
|
||
</div>
|
||
<div>
|
||
<a href="/Scratch/fr/validation/">Validation</a>
|
||
<a href="http://validator.w3.org/check?uri=referer"> [xhtml] </a>
|
||
.
|
||
<a href="http://jigsaw.w3.org/css-validator/check/referer?profile=css3"> [css] </a>
|
||
.
|
||
<a href="http://validator.w3.org/feed/check.cgi?url=http%3A//yannesposito.com/Scratch/fr/blog/feed/feed.xml">[rss]</a>
|
||
</div>
|
||
</div>
|
||
<div class="clear"></div>
|
||
</div>
|
||
</body>
|
||
</html> |