Erlang/OTP でデジタル署名の生成と検証
voluntas
Posted on January 21, 2020
証明書を作る
$ openssl genrsa -out private.key 4096
$ openssl rsa -in private.key -pubout -out public.key
メッセージに対する Signature の生成と検証
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false] [dtrace]
Eshell V5.10.4 (abort with ^G)
1> Msg = <<"Shiguredo Inc.">>.
<<"Shiguredo Inc.">>
2> {ok, RawPrivateKey} = file:read_file("private.key").
{ok,<<"-----BEGIN RSA PRIVATE KEY-----\nMIIJKQIBAAKCAgEA47g2Gof4U+Yt22heWwSreyfFv7w31i871U1CTB6J9x2Etq0e\nfWA2jaBORKc"...>>}
3> [PemEntryPrivateKey] = public_key:pem_decode(RawPrivateKey).
[{'RSAPrivateKey',<<48,130,9,41,2,1,0,2,130,2,1,0,227,184,
54,26,135,248,83,230,45,219,104,94,91,
4,...>>,
not_encrypted}]
4> PrivateKey = public_key:pem_entry_decode(PemEntryPrivateKey).
{'RSAPrivateKey','two-prime',
929014815589048078749758671332138349677423664245258492673537839224811943213021483236593144939725844906106637417573873281617666902906633781443629826462259419506415820618534241116244849064415559219610159387570774022078006650903460137892668224999805640212148179373129266896629044174846309275810038584592822164296488433267442026069250822426596112722181927552391005376548541466540418153544328623881553832845743017727459010371527290169176044233344985051875208149050174411175498530524269582754791986040459591915303228181707100077664561150513547751415909766580515530204356683701017348698848401053093776491233135327464981285299625053563308782243459400213768905455825585129169613401636101785679957287182224762514879776074983359226671124018788745968795424971855024815056947892650321883372757573297898716142362959119031533633589663659242562111565618925590801383008507698775531134023289742322160584143879876014489379821197671941028547527375615284991184946627575468277052010777749592384732196186551232633746207959454378719416464540085679867657050145550841781532521467253239308398376709881949855150591405769877706794405212853031989469484217146003271422451139154151628500056539088867118240138322685577257482853291339705561979442821151638075331141551,
65537,
922182260831980755345309525359907538310645840936803503988235716172088148156356143734937013352058605678727547754707544232046589260548866451803032460449857127057843374129709096477063470343249678884168315897255240612269171958962488354221394500470914993423582095749254992721061422841976694843185102463046615589641089079851281581487329481864629295102637369683091945843895286099543569326927175467699383319281929475231698825392064144864057670592188504242637619453689657694417230829962560961077147148204252540580894021840501631071798648483248529059437600284799356665966468163910000207815487171071440798779882686402005498535349508255889153683518722989269940802830815356598052766589846270823022160201059122011651724817496844419038971455649491168391172364094558276308283917833178812592075097459657246780804625854571808796887646748780912465637039583067411575015531882147627421006141126997591351227900337166988667962780071578976075351548440884862512466516657712801338584527685848753114974407129233342785093601780704922352027479386632108800033818086309581728018460864979311668312906925981414001821037900175341393161174768542667241554449269552213483132530818830461365898699841955014736751673968069063122519569662444380310582655835743411820490358753,
31043384292812077656298687421348284442301349338650912347766705789556791303873928650678353335192114113142062435308730906987261200089561716718032171526841806372416300581624737495527545535674244866548755311093709112466523859930134323083562920271597389477350908630095948736683836605743624868929069171771650554070890562827551080252641258208432430513309702156711674982718707042621511717261766490113662391509526120623852152509102186230557027277552624340555337268468086004097099221819936477785314135703775546170107794104487352129175069878817667724154256978886273001529794421592625430493439410760109348233961663388645162801393,
29926338147485945125681217783826194833781771331728690078559964731371750825414026966073416927943192162223920466607210975514433106971831917667142321165854017918967744944735078332695475871971838651995668185862671866977227399522872474121551298589901094689037748262466095247307796757121431995686263446424776497577149562929587064680118105379722773611971967661130038572010176592039646997347414143371954596947834042151685670276177022967636075702141639503909091532597571030557829116333442130037391579708924677720190476094418616333370262285987307376423509652740036653650310673604340597974532312968934075286708890816788872872607,
15853029365272054183148518708785638398384753035170495055542000844788237047134483483847340119660797337971505341790326166822553028740977639121703781324447893185101984342673859262711382791216345380375265006815756848437212880420857462577790924449544859102918659841494899252625763210415912188737935624774609773771221834333467805268709242560993279894097213967389815498445606104985836308548099560418146282543308668462686480006326685212388156139663567624848650803379616131118630952833813479996321062815416634560952861026427555442867864545283880912949855388091700639565423496679158153527845800072010309698250285339444532228161,
2866740175624712200726714455145350735713901466661469342710216497300026728137529354303811762418593472304831968038818842856395792385509876544765849707481751140505050624274025691939853785254729436154062664919753024717076363187277314990541206532911165791198544083369121961069294414471342143658061277090113170450117413919952814319571867274576187081129133359424056367473028802734713274171034163786702640640226164100100441552006337644244003894869237420167863598297870682665395901434935222734863425811566430058247338280982652140636564179492932476451268651294718252462222109783603922579369117610189177482185001091716138118837,
27402368073251631535126980912092052687380728690721038516082627957283182711607608549318041069919527912426299588078824520106345265623583351876982671335311928896342261028153794381928652280892170231693533543743714781232235219915141300404825332351692376892975439210023429269862747986109087959788210239097366874821378760533316634639817721548564572820931334680065826041578153017239112040225698742610572106502811620057350328633286510478618261925141247925435426026858885605049292498866935058387731560499180826420037871792459117062837852170451435258098548497907260876797064605455379256934897824390591387207155590431477364382800,
asn1_NOVALUE}
5> Signature = public_key:sign(Msg, sha, PrivateKey).
<<36,122,90,208,44,202,120,92,226,57,66,1,252,35,214,13,
214,115,71,11,21,70,212,126,121,72,115,38,237,...>>
6> {ok, RawPublicKey} = file:read_file("public.key").
{ok,<<"-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA47g2Gof4U+Yt22heWwSr\neyfFv7w31i871U1C"...>>}
7> [PemEntryPublicKey] = public_key:pem_decode(RawPublicKey).
[{'SubjectPublicKeyInfo',<<48,130,2,34,48,13,6,9,42,134,
72,134,247,13,1,1,1,5,0,3,130,
2,15,0,48,130,...>>,
not_encrypted}]
8> PublicKey = public_key:pem_entry_decode(PemEntryPublicKey).
{'RSAPublicKey',929014815589048078749758671332138349677423664245258492673537839224811943213021483236593144939725844906106637417573873281617666902906633781443629826462259419506415820618534241116244849064415559219610159387570774022078006650903460137892668224999805640212148179373129266896629044174846309275810038584592822164296488433267442026069250822426596112722181927552391005376548541466540418153544328623881553832845743017727459010371527290169176044233344985051875208149050174411175498530524269582754791986040459591915303228181707100077664561150513547751415909766580515530204356683701017348698848401053093776491233135327464981285299625053563308782243459400213768905455825585129169613401636101785679957287182224762514879776074983359226671124018788745968795424971855024815056947892650321883372757573297898716142362959119031533633589663659242562111565618925590801383008507698775531134023289742322160584143879876014489379821197671941028547527375615284991184946627575468277052010777749592384732196186551232633746207959454378719416464540085679867657050145550841781532521467253239308398376709881949855150591405769877706794405212853031989469484217146003271422451139154151628500056539088867118240138322685577257482853291339705561979442821151638075331141551,
65537}
9> public_key:verify(Msg, sha, Signature, PublicKey).
true
💖 💪 🙅 🚩
voluntas
Posted on January 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
undefined Unsafe Impedance: Safe Languages and Safe by Design Software, by Lee Barney and Adolfo Neto
November 2, 2024
rabbitmq RabbitMQ with Web MQTT Plugin vs. Node.js : Performance and Memory Usage Comparison
October 29, 2024
elixir New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane
October 25, 2024